random.randint function not working
Question:
This point of this game is to flip a coin 100 times and record either heads or tails. At the end of 100 flips, the program should print out the number of times it flipped heads and tails. The program is only printing heads when I run it and I feel that it is doing this by executing the first elif clause instead of the random.randint function.
Can anyone help complete this program??
import random
print ('Coin flip game')
start = input('Press enter to flip the coin')
coin_flip = random.randint(1, 2)
heads = int(1)
tails = int(2)
heads = int(heads)
tails - int(tails)
count = 0
while coin_flip:
count += 1
if count == 100:
break
elif coin_flip == 1:
print ('Heads')
elif coin_flip == 2:
print ('Tails')
Answers:
try something like this:
import random
print("Welcome to the coin flipper!")
start = input('Press enter to flip the coin')
count = 0
while True:
coin_flip = random.randint(1,2)
count+=1
if count >100:
break
if coin_flip == 1:
print('Heads')
elif coin_flip == 2:
print("Tails")
this way each iteration of the loop the number will change since its inside the loop
also you dont need to assign all those variables that you dont even use later
and you should use while True:
for the loop
How you want it printed is not clear. You could do:
>>> import random
>>> print [['H', 'T'][random.randint(0, 1)] for _ in range(100)]
['T', 'T', 'T', 'H', 'T', 'T', 'H', 'H', 'H', 'H', 'T', 'H', 'T', 'H', 'T', 'H', 'H',
'H', 'H', 'H', 'T', 'T', 'H', 'T', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'T', 'T',
'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H',
'H', 'H', 'T', 'H', 'T', 'H', 'T', 'H', 'H', 'H', 'T', 'T', 'T', 'H', 'H', 'H', 'T',
'H', 'H', 'T', 'T', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'H', 'T', 'T',
'H', 'H', 'H', 'H', 'T', 'H', 'H', 'H', 'T', 'T', 'T', 'H', 'H', 'H', 'T',]
or:
>>> heads = sum(random.randint(0, 1) for _ in range(100))
>>> print "heads =", heads, ' tails =', 100 - heads
heads = 46 tails = 54
Your issue is here:
while coin_flip: # coin_flip is set to 1 or 2
...
Since coin_flip
is set to 1 or 2, your loop will be infinite, and the important part is that coin_flip
won’t change (you are not updating it).
To fix this, you should update your coin_flip
by assigning it a random number inside the loop:
while True:
count += 1
coin_flip = random.randint(1, 2) # you need to "flip" coin inside the loop
if count == 100:
break
elif coin_flip == 1:
print ('Heads')
elif coin_flip == 2:
print ('Tails')
To count the number of heads, you could have a heads
and tails
variable to keep track:
heads = 0
tails = 0
while True:
count += 1
coin_flip = random.randint(1, 2) # you need to "flip" coin inside the loop
if count == 100:
break
elif coin_flip == 1:
print ('Heads')
heads += 1
elif coin_flip == 2:
print ('Tails')
tails += 1
print "Number of heads: %s, Number of tails: %s" % (heads, tails)
heads,tails,=0,0
import random
for i in range(100):
a=random.randint(1,2)
if(a%2==0):
tails+=1
else:
heads+=1
print 'no. of heads in 100 flips,',heads
print 'no. of tails in 100 flips,',tails
try this out
I know this is years ago but i fixed Serials’ code to count the total number of heads and tails at the end:
import random
print("Welcome to the coin flipper!")
start = input('Press enter to flip the coin')
count = 0
heads = 0
tails = 0
while True:
coin_flip = random.randint(1,2)
count+=1
if count >100:
break
if coin_flip == 1:
print('Heads')
heads += 1
elif coin_flip == 2:
print("Tails")
tails += 1
print(f'''
Number of tails: {tails}
Number of times heads {heads}
''')
You put random outside the loop .
It will execute only once.
This point of this game is to flip a coin 100 times and record either heads or tails. At the end of 100 flips, the program should print out the number of times it flipped heads and tails. The program is only printing heads when I run it and I feel that it is doing this by executing the first elif clause instead of the random.randint function.
Can anyone help complete this program??
import random
print ('Coin flip game')
start = input('Press enter to flip the coin')
coin_flip = random.randint(1, 2)
heads = int(1)
tails = int(2)
heads = int(heads)
tails - int(tails)
count = 0
while coin_flip:
count += 1
if count == 100:
break
elif coin_flip == 1:
print ('Heads')
elif coin_flip == 2:
print ('Tails')
try something like this:
import random
print("Welcome to the coin flipper!")
start = input('Press enter to flip the coin')
count = 0
while True:
coin_flip = random.randint(1,2)
count+=1
if count >100:
break
if coin_flip == 1:
print('Heads')
elif coin_flip == 2:
print("Tails")
this way each iteration of the loop the number will change since its inside the loop
also you dont need to assign all those variables that you dont even use later
and you should use while True:
for the loop
How you want it printed is not clear. You could do:
>>> import random
>>> print [['H', 'T'][random.randint(0, 1)] for _ in range(100)]
['T', 'T', 'T', 'H', 'T', 'T', 'H', 'H', 'H', 'H', 'T', 'H', 'T', 'H', 'T', 'H', 'H',
'H', 'H', 'H', 'T', 'T', 'H', 'T', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'T', 'T',
'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H',
'H', 'H', 'T', 'H', 'T', 'H', 'T', 'H', 'H', 'H', 'T', 'T', 'T', 'H', 'H', 'H', 'T',
'H', 'H', 'T', 'T', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'H', 'T', 'T',
'H', 'H', 'H', 'H', 'T', 'H', 'H', 'H', 'T', 'T', 'T', 'H', 'H', 'H', 'T',]
or:
>>> heads = sum(random.randint(0, 1) for _ in range(100))
>>> print "heads =", heads, ' tails =', 100 - heads
heads = 46 tails = 54
Your issue is here:
while coin_flip: # coin_flip is set to 1 or 2
...
Since coin_flip
is set to 1 or 2, your loop will be infinite, and the important part is that coin_flip
won’t change (you are not updating it).
To fix this, you should update your coin_flip
by assigning it a random number inside the loop:
while True:
count += 1
coin_flip = random.randint(1, 2) # you need to "flip" coin inside the loop
if count == 100:
break
elif coin_flip == 1:
print ('Heads')
elif coin_flip == 2:
print ('Tails')
To count the number of heads, you could have a heads
and tails
variable to keep track:
heads = 0
tails = 0
while True:
count += 1
coin_flip = random.randint(1, 2) # you need to "flip" coin inside the loop
if count == 100:
break
elif coin_flip == 1:
print ('Heads')
heads += 1
elif coin_flip == 2:
print ('Tails')
tails += 1
print "Number of heads: %s, Number of tails: %s" % (heads, tails)
heads,tails,=0,0
import random
for i in range(100):
a=random.randint(1,2)
if(a%2==0):
tails+=1
else:
heads+=1
print 'no. of heads in 100 flips,',heads
print 'no. of tails in 100 flips,',tails
try this out
I know this is years ago but i fixed Serials’ code to count the total number of heads and tails at the end:
import random
print("Welcome to the coin flipper!")
start = input('Press enter to flip the coin')
count = 0
heads = 0
tails = 0
while True:
coin_flip = random.randint(1,2)
count+=1
if count >100:
break
if coin_flip == 1:
print('Heads')
heads += 1
elif coin_flip == 2:
print("Tails")
tails += 1
print(f'''
Number of tails: {tails}
Number of times heads {heads}
''')
You put random outside the loop .
It will execute only once.