Python循环继续

2024-09-30 12:27:40 发布

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

我正在做一个飞镖记分员,但它总是一个接一个地转。我只是想知道为什么会这样。 代码:

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()函数来解决我的问题。在


Tags: importinputrawifdefsysintscore
2条回答

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

你的问题是:

threeorfive = 501

在整个游戏中,你开始你的每个功能

^{pr2}$

这意味着游戏“重置”后,两个玩家轮流。在

一个可能的解决方案是添加全局变量:

^{3}$

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

cumulative1 += minusnum1
cumulative2 += minusnum2

并将while循环更改为:

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

相关问题 更多 >

    热门问题