I'm trying to get an if statement to recognize a randomly generated number but I don't know what is going wrong, it just ends the program as soon as i hit start and does not print anything, so far I have this.
import random
random.randint(1,3)
if random.randint == ('1'):
print('message')
I have tried changing the random.randint(1,3) into a variable by making it "a == ('random.randint(1,3)" but that did not work either. Does anyone know whats going wrong with it?
P.S: Sorry if the question is asked badly, I don't use this site much.
CodePudding user response:
There are several problem with your code.
randint() creates a number like 1. It will never make a string like '1'. Additionally random.randint is a function. random.randint == ('1') will never be true, because a function is never never be equal to a number or a string. You want to compare the result of calling the function to an integer.
import random
num = random.randint(1,3)
if num == 1:
print('message')
CodePudding user response:
random.randint() is a function - therefore, you have to save its output in a variable, or else the value will be lost. For instance:
import random
rand_num = random.randint(1,3)
if rand_num == 1:
print('message')
