簡體   English   中英

程序在while循環結束

[英]Program ends at while loop

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循環時,程序剛剛結束。 我不知道為什么以及如何解決這個問題,有什么想法嗎? 而且我仍然不確定如何正確使用循環,因此我可能完全錯誤地使用了它,請讓我知道我做錯了什么以及如何解決此問題。

您在執行while True:循環while True:遇到了一個break ,該break 總是執行:

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

刪除它或將其移動到if語句中。

下一個問題是您永遠不會在循環中重新擲骰子。 您需要在游戲的每一輪中dice2dice1dice2分配新的隨機值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM