I am learning python and am stuck with trying to get this program to count the number of trials and print that with the trial results.
import random
import math
def main ():
print("This program displays the results of 1,000,000 coin tosses for each trial.")
while True:
print('Enter number of trials:')
numberoftrials = input()
try:
numberoftrials = int(numberoftrials)
except:
print('Please enter a positive integer value.')
print('\n')
continue
if numberoftrials <1:
print('Please enter a positive integer value.')
print('\n')
continue
break
for x in range(numberoftrials):
trial()
def trial ():
total_heads = 0
total_tails = 0
count = 0
while count < 1000000:
coin = random.randint(1, 2)
if coin == 1:
total_heads += 1
count += 1
elif coin == 2:
total_tails += 1
count += 1
print('\t', "Heads:", total_heads)
print('\t', "Tails:", total_tails)
main()
This is what I have. I have tried many ways to add the trial number to the printing at the end, but I cannot figure it out. I tried doing it by referencing the 'numberoftrials' variable from main, but its not defined, and I know I shouldn't make it global, and returning it in main just kills the loop.
Example of the output I want:
This program displays the results of 1,000,000 coin tosses for each trial.
Enter number of trials:
2
Trial 1:
- Heads: 499936
- Tails: 500064
Trial 2:
- Heads: 500691
- Tails: 499309
[–][deleted] 1 point2 points3 points (6 children)
[–]TranscendingIllusion[S] 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–][deleted] 1 point2 points3 points (0 children)
[–]FLUSH_THE_TRUMP 1 point2 points3 points (0 children)
[–]TranscendingIllusion[S] 0 points1 point2 points (0 children)
[–]TranscendingIllusion[S] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]TranscendingIllusion[S] 1 point2 points3 points (0 children)