I'm new to python and coding in general. I have an assignment for class to write a program that uses a loop to flip multiple coins multiple times. The code must:
The output should look something like:
How many coins do you want to flip? 2
How many times do you want to flip each coin? 2
Coin 1
Flip: 1; Result: Heads
Flip: 2; Result: Tails
Coin 2
Flip: 1; Result: Tails
Flip: 2; Result: Heads
There were 2 Tails in total
There were 2 Heads in total
Here's what I tried: (I'm having to use my phone due to some irl problems to ask this so if the format is off I'm sorry!)
import random
heads = 0
tails = 0
coins = int(input("How many coins: "))
while coins !="":
if coins \<= 0:
print ("Invalid input")
coins = int(input("How many coins: "))
else:
flips = int(input("How many times to flip: "))
if flips \<= 0:
print ("Invalid input")
flips = int(input("How many times to flip: "))
else:
for n in range (0, coins):
for i in range (0, flips):
print (" ")
print ("Coin %0d" %n)
print (" ")
n = coins + 1
for flip in range (0, flips):
flip = random.randint(0,1)
if flip == 0:
heads += 1
print ("Flips: %0d;" %i, "Result: heads")
else:
tails += 1
print ("Flip: %0d;" %i, "Result: tails")
i = flips + 1
print ("total heads:", heads)
print ("total tails:", tails)
break
What I get varies wildly by the numbers I input. I might flip 4 coins 3 times and it flips 6 coins. Or 1 coin 3 times and it flips 6 coins. But 1 coin 2 times flips 1 coin 2 times. The numbers counting the coins and flips also don't work right. I get results like:
Coin 0
Flip: 0; Result: Tails
Flip: 3; Result: Heads
Coin 2
Flip: 1; Result: Tails
Flip: 3; Result: Tails.
I'm stumped and at this point all the code looks like abc soup. Any help is appreciated.
I had put in the line 'for flip in range (0, flips)' not thinking that moving the 'for i in range (0, flips)' would tell the program the same thing. Which I wouldn't have figured out without Benjamin Davis or Scott Hunter and I greatly appreciate you both! So the middle part should actually be:
else:
for n in range (1, coins +1)
print (" ")
print ("Coin %0d" %n)
print (" ")
for i in range (1, flips +1)
flip = random.randit (0,1)
if flip == 0:
heads += 1
print ("Flip: %0d;" %i, "Result: heads")
else:
tails += 1
print ("Flip: %0d;" %i, "Result: tails")