'Coin Flip Simulator
How would I ask the user if they want to play and then start the simulation? And if they don't the program prints a goodbye message. And print the total number of times the coin was flipped at the end.
import random
def num_of_input():
userName = input("Please enter your name: ")
print("Hello " + userName + "!" + " This program simulates flipping a coin.")
while True:
try:
time_flip= int(input("How many times of flips do you want? "))
except:
print("Please try again.")
continue
else:
break
return time_flip
def random_flip():
return random.randint(0, 1)
def count_for_sides():
count_head=0
count_tail=0
times=num_of_input()
while True:
if count_head + count_tail == times:
break
else:
if random_flip()==0:
count_head+=1
else:
count_tail+=1
print()
print(str(count_head) + " came up Heads")
print(str(count_tail) + " came up Tails")
count_for_sides()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|