'Basic Python while/for question. Can't break out of a loop
Every function is working but can't quite get out of this whole function. even though I use break, something is not working. help, please.
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();
This is just basic python stuff that I'm trying to get started. I know I'm missing a small thing but quiet cannot get to the result.
On the very last while loop, It has Its comment in it. please read it for an additional explanation
Solution 1:[1]
Two separate things:
- Your logic at the very end is actually always
True
because you forgot to addor play_again == "yes
. - Right now your game asks if the user wants to play again after every single turn. Your check to play again should be tabbed in, so that it only executes one time after the game is played, not after each time.
When you're comparing statements, Python will attempt to make your values booleans. So, it will execute bool(insert-statement-here)
. So, at the very end, your comparison, if play_again == "1" or "yes":
is actually two parts.
bool(if play_again == "1")
- this has a value. It depends on if they put in play_again.
bool("yes")
- this is ALWAYS true. Since this is always true, your code will always enter this block.
Your head is in the right place, you should do:
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
I think you're displaying the health backwards as well. After the dragon strikes, you should display the character's health, not the dragon's. This is a long answer, but I think it resolves several of the issues you're encountering. You are definitely very close, but try to slow down just a bit. Your code will only be as clear as your thoughts on the problem - not more. You only need the playing of the game to be on a while true, everything else is just configuration, which can be done up front when the game starts. The "playing of the game" - which is the "damaging going back and forth" is the only thing that needs to be in the while.
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |