无限循环并没有得到预期的结果

时间:2013-12-10 09:46:50

标签: python

我有一个学校项目,我一直在努力,但我不能工作。我试过移动while True,但我得到一个无限循环,或者它到达最后一个输入,然后回到开头。

    import random
while True:
    dice1=random.randint (0,7)
    dice2=random.randint (0,7)
    break

strengthone = int(input ("Player 1, between 1 and 10 What do you want your characters strength to be? Higher is not always better."))
skillone = int(input ("Player 1, between 1 and 10 What do you want your characters skill to be? Higher is not always better."))
while True:
    if strengthone > 10:
        print ("Incorrect value")
    else:
        print ("Good choice.")
    if skillone > 10:
        print ("Incorrect value.")
    else:
        print ("Good choice.")

    strengthtwo = int(input ("Player 2, between 1 and 10 what do you want your characters strength to be? Higher is not always better."))
    skilltwo = int(input ("Player 2, between 1 and 10 what do you want your characters skill to be? Higher is not always better."))

    if strengthtwo > 10:
        print ("Incorrect value.")
    else:
        print ("Good choice.")
    if skillone > 10:
        print ("Incorrect value.")
    else:
        print ("Good choice.")
while True:
    strengthmod = strengthone - strengthtwo
    skillmod = skillone - skilltwo
    strengthmodone = strengthone - strengthtwo
    skillmodone = skillone - skilltwo
    strengthmodtwo = strengthone - strengthtwo
    skillmodtwo = skillone - skilltwo

    print ("Player 1, you rolled a", str(dice1))
    print ("Player 2, you rolled a", str(dice2))


    if dice1 == dice2:
        print ("")
    elif dice1 > dice2:
        strengthmodone = strengthmod + strengthone
        strengthmodone = strengthmod + strengthone
    elif dice2 > dice1:
        strengthmodtwo = strengthmod + strengthtwo
        skillmodtwo = skillmod + skilltwo
    elif dice1 < dice2:
        strengthmodone = strengthmod - strengthone
        skillmodone= skillmod - skillone
    else:
        dice2 < dice1
        strengthmodtwo = strengthmod - strengthtwo
        skillmodtwo = skillmod - skilltwo
    while True:
        strengthmodone = strengthmodone - dice1
        strengthmodtwo = strengthmodtwo - dice2
        break
        while True:
            if strengthmodone == 0:
                print ("Player one dies, well done player two. You win!")
            elif strengthmodtwo == 0:
                print ("Player two dies, well done player one. You win!")
            elif strengthmodone > 0:
                strengthmodone - 1
            else:
                strengthmodtwo == 0
                strengthmodtwo - 1
                break
                break

非常感谢任何帮助:)

1 个答案:

答案 0 :(得分:1)

有些事情肯定不起作用:

elif dice2 > dice1:
    strengthmodtwo = strengthmod + strengthtwo
    skillmodtwo = skillmod + skilltwo
elif dice1 < dice2: # you will never reach this code, because it is the same condition as the one above
    strengthmodone = strengthmod - strengthone
    skillmodone= skillmod - skillone

上述两个条件相同,反之亦然。

while True:
    strengthmodone = strengthmodone - dice1
    strengthmodtwo = strengthmodtwo - dice2
    break
    while True: # you will never reach this while loop, because of the break that stops the outer loop

您只需要运行一次代码就不需要循环。你做什么

while True:
    do_something
    break

正在执行do_something一次。

strengthmodtwo == 0 # it checks for equality, what you want is an assignment (=)

strengthmodtwo - 1 # you have to assign the newly computed value, either:
strengthmodtwo -= 1 # or
strengthmodtwo = strengthmodtwo - 1

您必须照顾不同的运营商。 比较运算符:==,&lt; =,&lt;,!= ... 赋值运算符:=, - =,+ = 计算运算符: - ,*,...(值本身没有任何反应)

如何在打印1到10的示例中使用循环:

# example 1
i = 1
while True:
    print i
    i += 1
    if i > 10:
        break


# example 2
i = 1
while i < 10:
     print i
     i += 1

示例2更好!

我希望我能帮助一点,我真的建议阅读一些教程,有数千种可用。