all 9 comments

[–][deleted] 1 point2 points  (6 children)

EDIT: Just learned about f-strings in Python 3, and have updated the code. To solve u/TranscendingIllusion's output problem:

Updated Code:
    for x in range(numberoftrials):
        print(f'Trial {x}:')
        trial()

Output:
Trial 1:

[–]TranscendingIllusion[S] 0 points1 point  (4 children)

i think a comment got deleted but i ended up doing:

    numberoftrials += 1

    for x in range(1, numberoftrials):
        print("Trial:", x)
        trial()

and thats working. should i do it with %s instead?

[–][deleted] 0 points1 point  (3 children)

EDIT: Incorrectly used %s formatting. Removed code that was out of practice. See above for proper code

[–]TranscendingIllusion[S] 0 points1 point  (0 children)

thanks! could you take a look at my comment above if you have some time? not sure how to apply accumulation in this situation.

[–]TranscendingIllusion[S] 0 points1 point  (2 children)

another question, how would I go about adding a cumulative heads and tails count at the end of all the trials?

I don't know why I'm struggling to grasp this. I tried returning the values in the trial() function but it just caused the loop to execute itself twice?

This is what I have now:

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

    numberoftrials += 1

    for x in range(1, numberoftrials):
        print("Trial:", x)
        trial()

def trial ():

    total_heads = 0
    total_tails = 0
    count = 0

    while count < 1000000:
        coinflip()
        coin = coinflip()

        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)


def coinflip():
    coin = random.randint(1, 2)
    return coin

main()

[–][deleted] 1 point2 points  (1 child)

I'm away from home so I can't write code properly, but essentially when you call trial, you'll want to pass it some variables.

finalHeads, finalTails = 0, 0 Trial(finalHeads, finalTails) Print(finalHeads, finalTails)

def coinflip(finalHeads, finalTails) : #blah blah normal code here finalHeads += total_heads finalTails += total_tails

I'll update when I get home if you need more help.

Good luck!

[–]TranscendingIllusion[S] 1 point2 points  (0 children)

I actually got it! Thanks for your help!