Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Help with Python coding
- BwnnyRxbbit
- Scratcher
100+ posts
Help with Python coding
Why doesn't this work?
Error:
19
Guess!1
Traceback (most recent call last):
File “<string>”, line 7, in <module>
TypeError: ‘>’ not supported between instances of ‘int’ and ‘str’
>>>
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:
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:
'35'
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
Oh, thank you! Inputs in Python are always strings. Print the input variable and you'll get something like this: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.'35'
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!")
Meow
- Discussion Forums
- » Advanced Topics
- » Help with Python coding