Hello there,
I want to emulate a coin flip and count how many heads and tails when flipping
it a hundred times.
I first coded coinflip_WRONG.py with "count_flips += 1" statement within the
if/else block.
When running it, either returned values are wrong or the script seems to enter
in an infinite loop showing no return values at all.
coinflip.py is a corrected version I worked out myself. I moved "count_flips+=
1" out of if/else block and inserted it before if/else.
However, I still don't understand the bug since, in my understanding, both
files are incrementing variable count_flips each time until the loop becomes
false.
Can somebody explain the reason of the bug.
Cheers,
Marc
#Coin Flip
#Flip a coin 100 times and count heads and tails
input('Press ENTER to flip a coin a hundred times')
import random
#set count values
count_heads = 0
count_tails = 0
count_flips = 0
while count_flips != 100:
coin_side = random.randint(1,2)
count_flips += 1
if coin_side == 1:
count_heads += 1
#count_flips += 1
else: count_tails += 1
#count_flips += 1
print('The coin returned',count_heads,'heads and',count_tails,'tails.')
#Coin Flip
#Flip a coin 100 times and count heads and tails
input('Press ENTER to flip a coin a hundred times')
import random
#set count values
count_heads = 0
count_tails = 0
count_flips = 0
while count_flips != 100:
coin_side = random.randint(1,2)
#count_flips += 1
if coin_side == 1:
count_heads += 1
count_flips += 1
else: count_tails += 1
count_flips += 1
print('The coin returned',count_heads,'heads and',count_tails,'tails.')
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor