0

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.

2 Answers 2

1

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.")
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain why it only works when you convert the input to an int?
@nephele strings comparison is entirely different from numbers comparison in python. When we compare 'a' > 'dfg' then it will return False as length of 1st string is less than 2nd.
0

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.

1 Comment

Thank you for taking time to explain this but I am not sure I understand?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.