程序以while循环结束

时间:2013-11-13 12:42:11

标签: python

import random


dice1=random.randint (1,6)
dice2=random.randint (1,6)


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."))

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.")

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))

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

程序刚到达while循环时结束。我不知道为什么以及如何解决这个问题,任何想法?而且我仍然不确定如何正确使用循环,所以我可能使用它完全错误,请让我知道我做错了什么以及如何解决这个问题。

1 个答案:

答案 0 :(得分:2)

break循环中的while True: 始终执行:

while True:
    if dice1 == dice2:
        # [ ... ]
    # [ ... ]
    # more `if` statements
    # [ ... ]
    if dice2 < dice1:
        # [ ... ]
    break

删除它或将其移到if语句中。

你的下一个问题是你永远不会在你的循环中重新掷骰子。您需要为每轮游戏继续为dice1dice2分配新的随机值。