Python Forum

Full Version: storing input as a integer help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import random
def function1():
a = random.randint(1,50)
b = random.randint(1,50)
c = a + b
print('what is',a,'plus',b,'?')
d = input()
if d > c:
print('your answer was incorrect, its a little too big')
if d < c:
print('your answer was incorrect, its a little higher')
else:
print('your answer was correct')
I just started coding and I dont understand whats wrong with this piece of code, whenever I try to execute the function, it says
'>' not supported between instances of 'str' and 'int'
but its a integer because I entered a number right? how do I fix this guys
input() always returns a str. It doesn't matter what you type, it's a str.
>>> x = input("?")
?43
>>> x
'43'
>>> type(x)
<class 'str'>
If you want a number, then you'll need to convert it to one first:
>>> x = int(input("?"))
?42
>>> x
42
>>> type(x)
<class 'int'>