我不知道如何从掷硬币游戏的循环中得到总数

2024-04-26 22:41:27 发布

您现在位置:Python中文网/ 问答频道 /正文

啊![掷硬币计划][1]

标题

我要做一个掷硬币程序,它能显示出掷硬币的次数和正面或反面的数量。用户有机会尽情发挥,直到他们退出程序。我应该得到这个人翻转的总次数,头部或尾部。例如,如果一个人玩了3次,第一次翻了10次,第二次翻了15次,第三次翻了20次,那么总共翻了45次。我无法让程序计算翻转、正面或反面的总数。同样,当玩家第一次玩游戏后,如果他们选择再次玩游戏,他们将无法输入低于先前数量的翻转次数。我不知道怎么了。在

#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."

Tags: thetoyouinputrawcounteroutcoin
3条回答

你的代码,重构了。在

#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()

您需要在两次运行之间将计数器设置为0

考虑重新构造代码以避免如此多的重复

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."

您也可以使用标准集合。计数器班级: (http://docs.python.org/dev/library/collections#collections.Counter

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

下面是一个使用常见python习惯用法的版本

^{pr2}$

相关问题 更多 >