real_python_noob
real_python_noob

Reputation: 15

Python - When number is equal to or above question

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

Answers (2)

Haytek
Haytek

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

Arco Bast
Arco Bast

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

Related Questions