Python Forum
New to the language. What am I doing incorrectly here?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to the language. What am I doing incorrectly here?
#1
1) I ask the user to input a number as a string.
2) I convert the string to an integer.
3) I perform a comparison to see if the user's number is a multiple of 10.

number = input("Provide a number: ")
user_input = int(number)
if user_input % 10 == 0:
	print("Your number is a multiple of 10.")
else:
	print("Your number is not a multiple of 10.")
I receive the following error:
Error:
Traceback (most recent call last): File "main.py", line 2, in <module> user_input = int(number) ValueError: invalid literal for int() with base 10: 'Provide a number: '
Reply
#2
Please use the python tags to show your code. It's much easier to understand.

Python here is declaring that the thing that was input was not an integer. We can't see your session to know what was wrong. If you want to test if it's really an integer rather than exiting if it's not, you can wrap the action in a try/except clause.

Also, you label the raw input as number, and after converting to an int(), you label it user_input. That seems backward to me. Maybe more like this:

import sys
user_input = input("Provide a number: ")
try:
    number = int(user_input)
except ValueError:
    print(f"{user_input} doesn't look like an integer to me.  Exiting")
    sys.exit()
if number % 10 == 0:
    print("Your number is a multiple of 10.")
else:
    print("Your number is not a multiple of 10.")
Reply
#3
assuming you enter a number, your code is working as expected.
The error you get, suggest that on line#1 you missed the input, i.e. I think you have line 1 like this:
number = ("Provide a number: ") # with or without the brackets
In other words - I think the code you run is not the same as the one you post here.


of course, as @bowlofred suggested, you can check the user input. No need for the sys.exit()
number = input("Provide a number: ")
try:
    user_input = int(number)
except ValueError:
    print(f"'{number}' is not a valid input.")
else:
    if user_input % 10 == 0:
        print("Your number is a multiple of 10.")
    else:
        print("Your number is not a multiple of 10.")
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Note: To make your code simpler, you could do
number = int(input("Provide a number: "))
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing to file ends incorrectly project_science 4 2,641 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  my openpyxl use is too slow, am I reading rows incorrectly? Clunk_Head 2 8,212 Apr-30-2020, 10:29 PM
Last Post: deac33
  list is printing incorrectly.. anna 1 2,051 May-18-2019, 12:12 PM
Last Post: ichabod801
  Code is Outputting Incorrectly mikerosz94 0 2,065 Sep-01-2017, 01:28 PM
Last Post: mikerosz94

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020