0

I have been banging my head against the wall for a few hours now trying to figure this out so any help is greatly appreciated. What I'm trying to do is loop the program upon a Y input for a Y/N question, specifically when Y is inputed I want it to react the way it does as shown in the sample output.

Here is my code:

import random
def main():
    name = eval(input("Hello user, please enter your name: "))
    print("Hello", name ,"This program runs a coin toss simulation")
    yn = input("Would you like to run the coin toss simulation?(Y/N):")
    if yn == Y:

    elif yn == N:
        print("Ok have a nice day!")

    heads = 0
    tails = 0
    count = tails + heads
    count = int(input("Enter the amount of times you would like the coin to     flip: "))
    if count <= 0: 
        print("Silly rabbit, that won't work")
    while tails + heads < count:
        coin = random.randint(1, 2)
        if coin ==1:

            heads = heads + 1
        elif coin == 2:

            tails = tails + 1
    print("you flipped", count , "time(s)")
    print("you flipped heads", heads , "time(s)")
    print("you flipped tails", tails , "time(s)")
main()

Here is the sample output that I'm looking for:

Hello user, please enter your name: Joe
Hello Joe this program runs a coin toss simulation
Would you like to run the coin toss simulation?(Y/N):Y
Enter the amount of times you would like the coin to flip:50
you flipped 50 times
you flipped heads 26 times
you flipped tails 24 times
Would you like to run another coin toss simulation?(Y/N):Y
Enter the amount of times you would like the coin to flip:100
you flipped 100 times
you flipped heads 55 times
you flipped tails 45 times
Would you like to run another coin toss simulation?(Y/N):N
Ok have a nice day!
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • You need quotes in line 6. Y is a variable "Y" is a string. Also there are other problems (you need to do something or just pass under line 6 etc...) But I assume you were asking this. And finally please write your code in your question next time, so it's easy to reproduce. – umutto Mar 29 '17 at 07:44
  • just put that code in, my bad I'm new here – will thompson Mar 29 '17 at 07:52

2 Answers2

0

I think you should say on line 6 if yn == 'Y' and not if yn == Y. You treat Y as a variable while it is actually a string from the input.

christinabo
  • 1,110
  • 1
  • 13
  • 18
0

To run your coin toss simulation multiple times you can put it inside a while loop.

Your if yn == Y: test won't work because you haven't defined the variable Y, so when Python tries to execute that line you get a NameError. What you actually should be doing there is to test the value of yn against the string 'Y'.

I've made a couple of other minor adjustments to your code. I got rid of that potentially dangerous eval function call, you don't need it. I've also made a loop that asks for the desired flip count; we break out ofthe loop when count is a positive number.

import random

def main():
    name = input("Hello user, please enter your name: ")
    print("Hello", name , "This program runs a coin toss simulation")
    yn = input("Would you like to run the coin toss simulation?(Y/N): ")
    if yn != 'Y':
        print("Ok have a nice day!")
        return

    while True:
        heads = tails = 0
        while True:
            count = int(input("Enter the amount of times you would like the coin to flip: "))
            if count <= 0: 
                print("Silly rabbit, that won't work")
            else:
                break

        while tails + heads < count:
            coin = random.randint(1, 2)
            if coin ==1:
                heads = heads + 1
            else:
                tails = tails + 1

        print("you flipped", count , "time(s)")
        print("you flipped heads", heads , "time(s)")
        print("you flipped tails", tails , "time(s)")

        yn = input("Would you like to run another coin toss simulation?(Y/N): ")
        if yn != 'Y':
            print("Ok have a nice day!")
            return

main()

This code is for Python 3. On Python 2 you should use raw_input in place of input, and you should also put

from future import print_function

at the top of the script so you get the Python 3 print function instead of the old Python 2 print _statement.


There are several improvements that can be made to this code. In particular, it should handle a non-integer being supplied for the count. This version will just crash if it gets bad input. To learn how to fix that, please see Asking the user for input until they give a valid response.

Community
  • 1
  • 1
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182