您好我正在尝试创建一个循环,以便当有人在我的代码中死亡时它会停止,同样代码也不会结束直到有人死亡(也就是说,有强度或技能达到零)然而,无论如何我投入了多少数字,游戏总是无限延续。
我该如何解决这个问题?
while True:
try:
strengthA=abs(int(input("enter strength A ")))
break
except ValueError:
print("Error")
print ("value entered")
while True:
try:
strengthB=abs(int(input("enter strength B ")))
break
except ValueError:
print("Error")
print ("value entered successfully")
while True:
try:
skillA=abs(int(input("enter skillA ")))
break
except ValueError:
print("Error")
print ("value entered successfully ")
while True:
try:
skillB=abs(int(input("enter skill B ")))
break
except ValueError:
print("Error")
print ("value entered successfully")
strengthmod=abs(int((strengthA-strengthB)/5))
print (strengthmod)
skillmod=abs(int((skillA-skillB)/5))
print(skillmod)
while strengthA>0 and strengthB>0:
print strengthA, strengthB
import random
throwA=int(random.randrange(6)+1)
print("Cloudy throws "+str(throwA))
throwB=int(random.randrange(6)+1)
print("Mocha throws "+str(throwB))
if throwA>throwB:
print("Cloudy wins!")
skillA=skillA+skillmod
strengthA=strengthA+strengthmod
skillB=skillB-skillmod
strengthB=strengthB-strengthmod
elif throwA<throwB:
print("Mocha wins!")
skillA=skillA-skillmod
strengthA=strengthA-strengthmod
skillB=skillB+skillmod
strengthB=strengthB+strengthmod
else:
print ("No one wins")
if skillA<1:
skillA=0
if skillB<1:
skillB=0
print("Cloudy new skill " +str(skillA))
print("Mocha new skill " +str(skillB))
print("Cloudy new strength " +str(strengthA))
print("Mocha new strength " +str(strengthB))
if strengthA<1:
print ("Cloudy dies")
if strengthB<1:
print ("Mocha dies")
print ("Battle ends")
答案 0 :(得分:0)
如果strengthA
和strengthB
彼此靠近,strengthmod
将为0
:
strengthmod=abs(int((strengthA-strengthB)/5))
所以strengthA
和strengthB
永远不会改变:
...
strengthB=strengthB-strengthmod
...
strengthA=strengthA-strengthmod
...
所以while
循环
while strengthA>0 and strengthB>0:
永远运行。
即使strengthA
为strengthB
,也许您应该确保strengthmod
和0
会发生变化,例如:
strengthB=strengthB-max(strengthmod, 1)