How come this script works as intended?

Viewed 32

So I started to look into python today and I made this script. It works exactly as intended but I don’t know why. So I generate a random number three times like shown below but I label all three the same, as random_number. Shouldn’t that mean all three only use the first randomly generated number? I’m sorry if this is a very silly question, I’m just curious.

random_number =random.randint(1,3)
print(random_number)

if 1 == random_number :print(name1)
if random_number == 2 :print(name2)
if random_number == 3 :print(name3)         
1 Answers

It first prints the random integer that was chosen by randint in line 2 then it checks to see if integer is among 1, 2, or 3 as specified and if found true just prints the name alongside it. And if randint randomly chooses numbers from 3 to 9 it just prints number in line 2 but not on any other line.

Related