0

Just started learning python through Zed's book "Learn python 3 the hard way". I am currently working on a text based game as an exercise. I got the game running but I could not figure out why the else statement on this specific function does not work. It does work when I take out the OR operator and only equate one choice on the first if statement.

def boss_floor():

    boss_hp = 100
    your_hp = 100

    while boss_hp > 0:
        print("Attack boss: 1.sword 2.arrow 3.magic")

        attack = input('> ')

        if attack == "sword" or "arrow" or "magic":
            boss_hp -= random.choice(attack_value)
            print("You attack the boss!")
            print(f"Boss hp: {boss_hp}\n")
            print("Big boss kicks you in the nuts")
            your_hp -= random.choice(attack_value)
            print(f"hp: {your_hp}\n")


        else:
            print("Input Invalid")
            print(f"Boss hp: {boss_hp}")

        if boss_hp <= 0:
            print("You win!")
            exit(0)

        if your_hp <= 0:
            print("You lose!")
            exit(0)
Sam
  • 1
  • 2
    `if attack == "sword" or "arrow" or "magic":` should be `attack == "sword" or attack == "arrow" or attack == "magic"`, the 2nd and 3rd strings alone will always evaluate to true. – FBergo Jun 20 '18 at 21:00

2 Answers2

0

I think you need to use:

if attack == "sword" or attack == "arrow" or attack == "magic"

since or "arrow" isn't a condition, just a string.

Lars
  • 710
  • 5
  • 19
0

In your if statement, you need to explicitly say for each or clause that it is comparing against attack. So,

if attack == "sword" or attack == "arrow" or attack == "magic":
     #rest of code here
Keveloper
  • 773
  • 5
  • 10