from random import randint
from time import sleep
fight = False
def start_up():
print("Fight Simulator - Test Stages.")
userInput = str(input("Enter your name "))
user_name = userInput
return user_name
def intro():
userName = start_up()
while True:
userInput = str(input("Welcome to the dojo, {}. To commence the first battle, type 'Start', or 'Begin'".format(userName)))
if userInput == "Start" or "start" or "Begin" or "begin":
return userInput
else:
print("Enter a valid response")
break
def fight_commence():
userInput = intro()
if userInput == "Start" or "start" or "Begin" or "begin":
fight = True
userHp = 100
opponentHp = 100
while fight == True:
userDmg = randint(0,100)
opponentDmg = randint(0,100)
opponentHp -= userDmg
if opponentHp <= 0:
opponentHp == 0
print("You did {} damage".format(userDmg))
sleep(1)
print("Opponent has {} hp remaining".format(opponentHp))
sleep(1)
if opponentHp <= 0:
print("You killed him!")
fight = False
else:
print("Enter a valid action")
fight_commence()
因此,出於某種原因,當給出「開始」或「開始」或「開始」或「開始」以外的響應時,代碼不會打印(「輸入有效響應」)。我希望它循環回到用戶輸入階段,但它只是繼續執行程序,就好像一切正常。我無法弄清楚我弄亂了什麼。到目前爲止我的代碼有兩個問題。
其次,我希望opponentHp自動等於0時0以下。例如,該健康驟降時opponentHp = -45,opponentHp應等於0。
任何幫助理解。
可能會幫助你,如果你標記的語言.... – epascarello
廢話,它的蟒蛇。 –
「我的代碼到目前爲止的兩個問題」並不是一個非常具體的/可搜索的標題。你的問題是一個經典的問題:'如果userInput ==「開始」或「開始」或「開始」或「開始」:'沒有做你認爲的事情......(提示:用戶輸入==「開始」)或「開始」或「開始」或「開始」:') –