简体   繁体   中英

I can't figure out how to get total number from loops in flip coin game

![flip coin program][1]

Heading

I have to make this flip coin program which puts out the number of flips and number of heads or tails. The user gets a chance to play as much as they want until they quit the program. I'm supposed to get the total number of times the person flips and gets heads or tail. For example if a person plays 3 times, first time they flip 10 times, second time they flip 15 times and third time they flip 20 times, then the total amount of flips would be 45 flips. I can't get the program to count the total for flips, heads or tails. Also after the person plays for the first time, if they choose to play again they can't enter the number of flips that's lower than the previous amount. I can't figure out what's wrong.

#coin toss

print "Welcome to my coin tossing game. I will flip a coin the amount"
print "of times you tell me to and show you your results."

import random
counter = 0
start = 0
user_input = 0
heads = 0
tails = 0

user_input = int(raw_input("Enter the number of times you want to flip the coin "))

while user_input > counter:
        chance = random.randrange(2)
        counter = counter + 1
        if chance == 1:
            heads = heads + 1
        else:
            tails = tails + 1

print "You flipped the coin", counter, "times."
print heads, "times came out heads"
print tails, "times came out tails"

headstotal = heads
tailstotal = tails

restart = raw_input("Would you like to try again? y or n ")
while restart == "y":
        user_input = int(raw_input("Enter the number of times you want to flip the coin"))
        while user_input > counter:
                chance = random.randrange(2)
                counter = counter + 1
                if chance == 1:
                        heads = heads + 1
                else:
                       tails = tails + 1
        print "You flipped the coin", counter, "times."
        print heads, "times came out heads."
        print tails, "times came out tails."

        restart = raw_input("Would you like to try again? y or n ")
        print "Thanks for playing."

You need to set the counter to 0 between runs

Consider restructuring your code to avoid so much duplication

while True:
    counter = 0
    user_input ...
        ...
    ...
    restart = raw_input("Would you like to try again? y or n ")
    if restart == "n":
        break
print "Thanks for playing."

You can also use the standard collections.Counter class: ( http://docs.python.org/dev/library/collections#collections.Counter )

c = Counter()
c.update(["head"])

Here is a version using common python idioms

#coin toss

print "Welcome to my coin tossing game. I will flip a coin the amount"
print "of times you tell me to and show you your results."

import random, collections

while True:
    counter = collections.Counter()    
    user_input = int(raw_input("Enter the number of times you want to flip the coin "))

    counter.update(random.choice("head", "tail") for i in range(user_input))

    print "You flipped the coin", user_input, "times."
    print counter["head"], "times came out heads"
    print counter["tail"], "times came out tails"

    restart = raw_input("Would you like to try again? y or n ")
    if restart != "y":
        break
print "Thanks for playing."

Your code, refactored.

#coin toss

print "\t\tWelcome to my coin tossing game. I will flip a coin the amount"
print "\t\tof times you tell me to and show you your results.\n"

import random

heads = 0
tails = 0
headstotal = 0
tailstotal = 0

while True:
    counter = user_input = int(raw_input("Enter the number of times you want to flip the coin "))
    while user_input > 0:
        chance = random.randrange(2)
        if chance==1:
            heads+=1
            headstotal+=1
        else:
            tails+=1
            tailstotal+=1
        user_input-=1

    print "You flipped the coin", counter, "times."
    print heads, "times came out heads"
    print tails, "times came out tails"
    heads=tails=0
    restart = raw_input("Would you like to try again? y or n")
    if restart=="n":
        break
print "total heads = ",headstotal
print "total tails = ",tailstotal   
print "total flips = ", headstotal+tailstotal
print "Thanks for playing."
raw_input()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM