Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 循环代码:无限如何停止此操作_Python_Loops_Infinite - Fatal编程技术网

Python 循环代码:无限如何停止此操作

Python 循环代码:无限如何停止此操作,python,loops,infinite,Python,Loops,Infinite,嗨,我试图创建一个循环,这样当有人死在我的代码中时,它会停止,同样,代码也不会结束,直到有人死了。也就是说,强度或技能达到零。然而,无论我输入什么数字,游戏总是无限继续 我如何解决这个问题 while True: try: strengthA=abs(int(input("enter strength A "))) break except ValueError: print("Error") p

嗨,我试图创建一个循环,这样当有人死在我的代码中时,它会停止,同样,代码也不会结束,直到有人死了。也就是说,强度或技能达到零。然而,无论我输入什么数字,游戏总是无限继续

我如何解决这个问题

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")
如果strengthA和strengthB彼此接近,strengthmod将为0:

所以strengthA和strengthB永远不会改变:

...
strengthB=strengthB-strengthmod
...
strengthA=strengthA-strengthmod
...
所以while循环

永远运行

也许您应该确保strengthA和strengthB将发生变化,即使strengthmod为0,例如:


没有缩进是不可能读懂正在发生的事情的。请您修复它,或者将您的代码上传到?实现这一点最简单的方法是设置一个变量,该变量在某人死亡的关键条件下发生变化。然后替换上部while循环以检查此变量的状态。
...
strengthB=strengthB-strengthmod
...
strengthA=strengthA-strengthmod
...
while strengthA>0 and strengthB>0:
strengthB=strengthB-max(strengthmod, 1)