nephele
nephele

Reputation: 15

While loop condition not triggered by user input

I am working on a little game to work on my Python skills. As you can see below, I am stuck on getting the program to continue after the user inputs an erroneous value.

One of the things I tried is getting rid of the while loop and wrapping the if statement in a recursive function. However, I am not sure if this is the right way, and it's kind of above my level at the moment. Any thoughts would be very welcome!

print("Greetings, weary wanderer.")
print("Welcome to Freyjaberg. Choose your weapon.")
#user able to select from list, needs input and to check which they pick

weapon_choice = (input("You can choose between three weapons to defeat the beast!"
                   " Press 1 for Axe, 2 for Crossbow, 3 for Sword."))

while True:
    weapon_choice <= '4'
    if weapon_choice=='1':
        print("You chose the Axe of Might")
        break
    elif weapon_choice=='2':
        print("You chose the Sacred Crossbow")
        break
    elif weapon_choice=='3':
        print("You chose the Elven Sword")
        break
    else:
        weapon_choice >= '4'
        print("Wanderer, there is no such weapon. Please choose from the ones available.")
        input("You can choose between three weapons to defeat the beast! "
              "Press 1 for Axe, 2 for Crossbow, 3 for Sword.")

If I enter 1, 2 or 3, it works fine!

Greetings, weary wanderer.
Welcome to Freyjaberg. Choose your weapon.
You can choose between three weapons to defeat the beast! Press 1 for Axe, 2 for Crossbow, 3 for Sword.2
You chose the Sacred Crossbow
Now you must enter the first dungeon. Prepare yourself!

Process finished with exit code 0

If I enter 4 or above, it does show the right error message I expect. So far so good. Then it prompts me again (as expected). But when I type 1, 2, or 3 now, it doesn't understand and continue. It just keeps telling me it's wrong.

Greetings, weary wanderer.
Welcome to Freyjaberg. Choose your weapon.
You can choose between three weapons to defeat the beast! Press 1 for Axe, 2 for Crossbow, 3 for Sword.4
Wanderer, there is no such weapon. Please choose from the ones available.
You can choose between three weapons to defeat the beast! Press 1 for Axe, 2 for Crossbow, 3 for Sword.1
Wanderer, there is no such weapon. Please choose from the ones available.
You can choose between three weapons to defeat the beast! Press 1 for Axe, 2 for Crossbow, 3 for Sword.

Upvotes: 0

Views: 169

Answers (2)

Shan Ali
Shan Ali

Reputation: 564

Take user input in while loop:

while True:
    weapon_choice = (input("You can choose between three weapons to defeat the beast!"
               " Press 1 for Axe, 2 for Crossbow, 3 for Sword."))
    try:
        weapon_choice = int(weapon_choice)
    except Exception as e:
        print ("Please enter a valid number!")
        continue
    if weapon_choice==1:
        print("You chose the Axe of Might")
        break
    elif weapon_choice==2:
        print("You chose the Sacred Crossbow")
        break
    elif weapon_choice==3:
        print("You chose the Elven Sword")
        break
    else:
        print("Wanderer, there is no such weapon. Please choose from the ones  available.")

Upvotes: 1

kuco 23
kuco 23

Reputation: 830

You have to revalue the weapon_of_choice variable inside the while loop, as in weapon_of_choice = input("You can choose between three weapons to defeat the beast! Press 1 for Axe, 2 for Crossbow, 3 for Sword."). The call to input only asks for user input, and returns the input value, you have to decide what to do with it.

Upvotes: 0

Related Questions