New To Python - Random Number With Printed Message On Certain Numbers


I am learning Python to add to my Sys Admin skills.

My program rolls a 20 sided dice and is supposed to say “Winner” on a 20 and “Looser” on a 1.

It is printing the random numbers, but not printing “Winner” or “Looser” on a 20 or 1.

#import module
import random

roll = print(random.randint(1,20))

if roll == 20:

if roll == 1:

Any help is greatly appreciated.


The problem is here:

roll = print(random.randint(1,20))

What this is doing is generating a random number in the desired range, and passing it to print. Then, the value returned by print is assigned to the name roll.

Well, what is the return value of print? print doesn’t actually return anything, which translates to returning the value None. It doesn’t attempt to return the value passed to it, or anything like that. So the above is equivalent to:

roll = None

Written that way, it’s obvious that roll is never going to compare equal to 20 or 1.

The correct solution is simple: assign the variable and do the printing separately. As in

roll = random.randint(1,20)

Thank you for the quick response. I am very, very green, but hope to grow with the help of this community!

In addition to @Zeturic excellent answer you could also read Documentation > The Python Tutorial > 4. More Control Flow Tools > 4.7 Defining Functions

I am just starting a Python class, and will reference the link above, thank you!