"Sudden Death" 函数在实际执行任何操作之前循环不止一次

"Sudden Death" function loops more than once before actually doing anything

我的代码中的函数和循环有点问题。基本上问题是 "suddendeath" 函数应该只 运行 一次并完成游戏。但是它会循环大约 3-5 次然后结束游戏。

这是完整代码的 pastebin link:http://pastebin.com/zTv35W8N

但是,这是导致问题的函数:

def suddendeath():
    global charone_strength
    global chartwo_strength
    print "\n============"
    print "SUDDEN DEATH"
    print "============"
    time.sleep(1)
    player1 = random.randint(1,6)
    player2 = random.randint(1,6)
    print "\n"+str(charone)+" has rolled:",player1
    time.sleep(0.5)
    print "\n"+str(chartwo)+" has rolled:",player2
    if player1 == player2:
        print "\nIt's a draw!"
        suddendeath()
    if player1 > player2:
        chartwo_strength = 0
    elif player2 > player1:
        charone_strength = 0

这里是循环:

while charone_strength > 0 or chartwo_strength > 0:
    if counter == 220:
        print "\nRound 220 has been reached, Sudden Death Mode active!"
        suddendeath()
    else:
        mods()
        battle(charone_strength, chartwo_strength, charone_skill, chartwo_skill, strength_mod, skill_mod)
else:
    if charone_strength <= 0:
        print "\n"+str(charone)+" has died!",chartwo,"wins!"
        time.sleep(1)
    elif chartwo_strength <= 0:
        print "\n"+str(chartwo)+" has died!",charone,"wins!"
        time.sleep(1)
    play = raw_input("Would you like to play again? y/n: ")
    if play in ["yes","y","Yes","Y"]:
        execfile("gcse.py")
    else:
        print "Goodbye! Thanks for playing!"
        exit()

我确实建议先阅读完整代码,但如果您有任何见解和进一步帮助解决此问题,我们将不胜感激。干杯:)

如果suddendeath应该结束游戏,你应该在它执行后退出循环,例如:

...
if counter == 220:
    print "\nRound 220 has been reached, Sudden Death Mode active!"
    suddendeath()
    break

现在的问题是,如果break发生,while循环的else部分将不会被执行。据我了解,这段代码应该在任何情况下都执行,所以只写在 while 之后而不是 else.

P.S。还有其他问题,比如 counter 没有在任何地方定义或更新,但我猜这是因为你没有 post 完整的工作代码。

编辑:忽略这个答案,让它成为历史。真正的问题似乎在条件

while charone_strength > 0 or chartwo_strength > 0:

应该是

while charone_strength > 0 and chartwo_strength > 0:

如果一个玩家达到强度0而不是两个,则游戏结束。