Discuss Scratch

BwnnyRxbbit
Scratcher
100+ posts

Help with Python coding

Why doesn't this work?
import random

randomNumber = random.randint(1, 100)
print(randomNumber)
userGuess = input("Guess!")

if randomNumber > userGuess:
print("Your guess is too small!")

Error:
19
Guess!1
Traceback (most recent call last):
File “<string>”, line 7, in <module>
TypeError: ‘>’ not supported between instances of ‘int’ and ‘str’
>>>


Meow
Knightbot63
Scratcher
1000+ posts

Help with Python coding

Inputs in Python are always strings. Print the input variable and you'll get something like this:
'35'
This isn't actually an integer the way Python sees it Python sees it as a String. You can force the userGuess variable to be an integer by using the int() function that's in Python. You can also do this with strings and many more.
Here is your fixed code:
import random
randomNumber = random.randint(1, 100)
print(randomNumber)
userGuess = int(input("Guess!"))
if randomNumber > userGuess:
    print("Your guess is too small!")

Working on an RPG. Consistently losing followers because of inactivity.
I head to forums time-to-time. Not as much as I used to.
BwnnyRxbbit
Scratcher
100+ posts

Help with Python coding

Knightbot63 wrote:

Inputs in Python are always strings. Print the input variable and you'll get something like this:
'35'
This isn't actually an integer the way Python sees it Python sees it as a String. You can force the userGuess variable to be an integer by using the int() function that's in Python. You can also do this with strings and many more.
Here is your fixed code:
import random
randomNumber = random.randint(1, 100)
print(randomNumber)
userGuess = int(input("Guess!"))
if randomNumber > userGuess:
    print("Your guess is too small!")
Oh, thank you!


Meow

Powered by DjangoBB