Python循环继续

时间:2017-02-26 09:54:02

标签: python python-2.7

我正在做一个飞镖得分守门员,但它只是一直四处奔走。我只是需要一些帮助,为什么会这样。 代码:

import time
import sys
from sys import argv
script, name1, name2 = argv
def dartscore():
    print "Play from 501 or 301?"
    threeorfive = int(raw_input())
    if (threeorfive == 501):
        def playerone():
            startnum1 = threeorfive
            while (startnum1 > 0):
                print "Ready ", name1,"?"
                print "Please enter your score."
                minusnum1 = int(raw_input())
                startnum1 = startnum1 - minusnum1
                playertwo()
            if (startnum1 == 0):
                print "Well done! You win!"
            elif (startnum1 < 0):
                print "Sorry but you have entered a wrong score"
                playertwo()

        def playertwo():
            startnum2 = threeorfive
            print "Ready ", name2,"?"
            print "Please enter your score."
            minusnum2 = int(raw_input())
            startnum2 = startnum2 - minusnum2
            if (startnum2 == 0):
                print "Well done! You win!"
                print "Unlucky ", name1,". Well played though."
                sys.exit()
            if (startnum2 < 0):
                print "Sorry but you have entered an incorrect score. Please try again"
                startnum2 += minusnum2
            playerone()
        playerone()
dartscore()

现在两个函数playerone()playertwo()是不同的,因为我正在尝试使用playerone()函数来查看是否解决了我的问题。

2 个答案:

答案 0 :(得分:1)

你有一个while(startnum1 > 0):。似乎startnum1总是大于0。退出循环的唯一方法是玩家2在{0}上有startnum2

答案 1 :(得分:0)

你的问题是:

threeorfive = 501

在整个游戏过程中,您可以使用

开始每个功能
startnum = threeorfive

这意味着游戏重置&#39;两名球员转身后。

可能的解决方法是添加全局变量:

cumulative1 = 0
cumulative2 = 0

然后在每次迭代中更新累积:

cumulative1 += minusnum1
cumulative2 += minusnum2

并将while循环更改为:

while(threeorfive - cumulative1 > 0)
while(threeorfive - cumulative2 > 0)