Reputation: 15
I am trying to write a simple script that will give a random number between 1 and 100 and print out either "you win" or "you lose" based on the result. When testing for the specific number like == 1
it works fine, but when replacing it with <=
it gives me this error: TypeError: '<' not supported between instances of 'NoneType' and 'int'
Here is my code:
import random
number = print(random.randint(1, 100))
if number <= 20:
print("you win")
else:
print("you lose")
Upvotes: 0
Views: 58
Reputation: 101
You have to first store the variable you get from the random:
number = random.randint(1, 100)
You can they print it, and compare it in your "if":
print(number)
if number <= 20:
...
The print function does not return the variable you want.
Upvotes: 0
Reputation: 3892
print
always returns None
, so don't assign the return value of print
to number
. Do it in two steps:
import random
number = random.randint(1, 100)
print(number)
if number <= 20:
print("you win")
else:
print("you lose")
Upvotes: 5