Why doesn’t this pick heads? The coin looks fine but isn’t working, maybe the randrange
is broken?
import random
coin = 0
coin = random.randrange(1,2)
if coin == 1:
print("tails")
if coin == 2:
print("heads")
>Solution :
You have to write
coin = random.randrange(1,3)
now coin will be 1 or 2. Let’s see an example where we are generating a random integer number within a given range. This example shows all the different forms of random.randrange() function.
import random
# Random number between 0 and 15
n1 = random.randrange(16)
print(n1)
# Random number between 10 and 30
n2= random.randrange(10, 31)
print(n2)
# Random number between 20 and 100 divisible by 2
n3= random.randrange(20, 101, 2)
print(n3)
Also you can see here