我的代码中有一个函数和一个循环出现了一些问题。基本上问题是,“突然死亡”的功能应该只运行一次,并完成游戏。然而,它循环约3-5次,然后完成游戏。
下面是完整代码的pastebin链接: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()我确实建议先阅读完整的代码,但是对于解决这个问题,任何见解和进一步的帮助都将不胜感激。干杯:)
发布于 2015-03-17 23:49:34
如果suddendeath应该完成游戏,则应该在循环执行后退出,例如:
...
if counter == 220:
print "\nRound 220 has been reached, Sudden Death Mode active!"
suddendeath()
break现在的问题是,如果发生else,则不会执行while循环的break部分。据我所知,在任何情况下都应该执行这段代码,所以只需在没有while的else之后编写它。
还有其他问题,比如counter没有在任何地方被定义或更新,但我想这是因为您没有发布完整的工作代码。
编辑:忽略了这个答案,只是把它留给了历史。真正的问题似乎在这种情况下
while charone_strength > 0 or chartwo_strength > 0:这应该是
while charone_strength > 0 and chartwo_strength > 0:因此,游戏结束时,如果其中一名球员达到强度0,而不是两者都。
https://stackoverflow.com/questions/29110870
复制相似问题