Q: assignment that flips a coin 100 times and gives you the total amount of heads and tails.
A: I started by making it flip a coin 5 times so I could easily watch it working and fix bugs.
I don't think it actually flips the coin exactly 100 times, can you spot the error?
It took me an hour to get it to work:
import random
def variables ():
heads = 0
tails = 0
coinCount = 0
againPlay = "y"
def game():
heads = 0
tails = 0
againPlay = "y"
coinCount = 1
while coinCount > 0:
if againPlay != "y":
print ("you had ", heads, "heads.")
print (" and ", tails, "tails.")
end = input ("You're all done now!")
nmCoin = random.randrange(2)
if coinCount > 100: againPlay = "n"
if nmCoin == 1:
heads = heads + 1
coinCount = coinCount + 1
elif nmCoin == 0:
tails = tails + 1
coinCount = coinCount + 1
else:
print ("you had ", heads, "heads.")
print (" and ", tails, "tails.")
end = input ("You're all done now!")
variables ()
game ()