import random NUMBER_OF_TRIALS = 1000000 def toss(): return random.choice("HT") ##count the number of tosses until a head occurs and return ## that number def do_trial(): chocula = 1 while toss() != "H": chocula += 1 return chocula def do_big_experiment(): tally = {} for k in range(NUMBER_OF_TRIALS): trial = do_trial() if trial not in tally: tally[trial] = 1 else: tally[trial] += 1 return tally d = do_big_experiment() sorted_keys = sorted(d.keys()) for key in sorted_keys: print(f"{key}\t\t{d[key]}") total = 0 for key in d: total += key*d[key] print(f"average is {total/NUMBER_OF_TRIALS}")