I just started to learn Python and am trying to code the following question:
Coin Flip Simulation- Write some code that simulates flipping a single coin however many times the user decides. The code should record the outcomes and count the number of tails and heads.
The following is my code:
import random
def num_of_input():
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(count_head)
print(count_tail)
The issue I am having right now is: if i give the input as x (x times of flip), then I need to give input X+1 times to be able to see result, something like this:
count_for_sides()
how many times of flips do you want?4
how many times of flips do you want?4
how many times of flips do you want?4
how many times of flips do you want?4
how many times of flips do you want?4
0
4
I am really confused about this situation. I think this means my input function is in a while loop, so it keeps on checking the conditions as it it continues to ask for my input.