[英]Basic Python while/for question. Can't break out of a loop
每個 function 都在工作,但不能完全擺脫整個 function。即使我使用 break,也有一些東西不工作。 請幫忙。
def start():
wizard = "Wizard"
elf = "Elf"
human = "Human"
orc = "Orc"
wizard_hp = 70
elf_hp = 100
human_hp = 150
orc_hp = 400
wizard_damage = 150
elf_damage = 100
human_damage = 20
orc_damage = 200
dragon_hp = 300
dragon_damage = 50
while True:
print("✰✰ Welcome to the game ✰✰\n")
print("1) Wizard ")
print("2) Elf ")
print("3) Human ")
print("4) Orc ")
character = input("Choose your character: ").lower()
if character == "1" or character == "wizard":
character = wizard
my_hp = wizard_hp
my_damage = wizard_damage
break
elif character == "2" or character == "elf":
character = elf
my_hp = elf_hp
my_damage = elf_damage
break
elif character == "3" or character == "human":
character = human
my_hp = human_hp
my_damage = human_damage
break
elif character == "4" or character == "orc":
character = orc
my_hp = orc_hp
my_damage = orc_damage
break
else:
print("\n- Unknown Character \n")
break
print("\nYou have chosen the character: ", character)
print("\n- Health: ", my_hp)
print("- Damage: ", my_damage, "\n")
while True:
dragon_hp =- my_damage
print("The", character, "damaged the Dragon!")
print(f"The {character}'s hitpoints are now: {my_hp} \n")
if dragon_hp <= 0:
print("The Dragon has lost the battle", "\n")
my_hp =- dragon_damage
print("The Dragon strikes back at", character)
print("The Dragon's hitpoints are now: ", dragon_hp, "\n")
if my_hp <= 0:
print("You have lost your battle! ", "\n")
play_again = input("Do you want to play again? Type Yes or No: ").lower()
while True:
if play_again == "1" or "yes":
print("\n") # even if you only put start(); on line 80, the line 81 and 83 is reading line 80(start(;)) for some reasons
start();
elif play_again == "2" or "no":
break
else:
break
break
start();
這只是基本的 python 東西,我正試圖開始。 我知道我錯過了一件小事,但安靜無法得到結果。
在最后一個 while 循環中,它有它的評論。 請閱讀它以獲得額外的解釋
兩個不同的東西:
True
因為您忘記添加or play_again == "yes
。 當您比較語句時,Python 將嘗試使您的值成為布爾值。 因此,它將執行bool(insert-statement-here)
。 所以,最后,你的比較, if play_again == "1" or "yes":
實際上是兩部分。
bool(if play_again == "1")
- 這有一個值。 這取決於他們是否再次播放。
bool("yes")
- 這總是正確的。 由於這始終為真,因此您的代碼將始終進入此塊。
你的頭在正確的地方,你應該這樣做:
if play_again == "1" or play_again == "yes":
print("\n") # even if you only put start(); on line 80, the line 81 and 83 is reading line 80(start(;)) for some reasons
elif play_again == "2" or play_again == "no":
break
else:
break
我認為您也在向后顯示健康狀況。 巨龍攻擊后,你應該顯示角色的生命值,而不是巨龍的生命值。 這是一個很長的答案,但我認為它可以解決您遇到的幾個問題。 你肯定非常接近,但試着放慢一點。 您的代碼只會像您對問題的想法一樣清晰 - 不會更多。 你只需要游戲的播放時間是真實的,其他一切都只是配置,可以在游戲開始時預先完成。 “玩游戲”——也就是“破壞性的來回走動”是這期間唯一需要做的事情。
def start():
wizard = "Wizard"
elf = "Elf"
human = "Human"
orc = "Orc"
wizard_hp = 70
elf_hp = 100
human_hp = 150
orc_hp = 400
wizard_damage = 150
elf_damage = 100
human_damage = 20
orc_damage = 200
dragon_hp = 300
dragon_damage = 50
print("✰✰ Welcome to the game ✰✰\n")
print("1) Wizard ")
print("2) Elf ")
print("3) Human ")
print("4) Orc ")
character = input("Choose your character: ").lower()
if character == "1" or character == "wizard":
character = wizard
my_hp = wizard_hp
my_damage = wizard_damage
elif character == "2" or character == "elf":
character = elf
my_hp = elf_hp
my_damage = elf_damage
elif character == "3" or character == "human":
character = human
my_hp = human_hp
my_damage = human_damage
elif character == "4" or character == "orc":
character = orc
my_hp = orc_hp
my_damage = orc_damage
else:
print("\n- Unknown Character \n")
return
print("\nYou have chosen the character: ", character)
print("\n- Health: ", my_hp)
print("- Damage: ", my_damage, "\n")
while True:
dragon_hp -= my_damage
print("The", character, "damaged the Dragon!")
print("The Dragon's hitpoints are now: ", dragon_hp, "\n")
if dragon_hp <= 0:
print("The Dragon has lost the battle", "\n")
play_again = input("Do you want to play again? Type Yes or No: ").lower()
break
my_hp -= dragon_damage
print("The Dragon strikes back at", character)
print(f"The {character}'s hitpoints are now: {my_hp} \n")
if my_hp <= 0:
print("You have lost your battle! ", "\n")
play_again = input("Do you want to play again? Type Yes or No: ").lower()
break
if play_again == "1" or play_again == "yes":
print(
"\n") # even if you only put start(); on line 80, the line 81 and 83 is reading line 80(start(;)) for some reasons
start()
start()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.