0

I am making a little text based game to practise my python skills. I am struggling to get the if statement to show the correct result based on what my user inputs.

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

if input(1):
    print("You chose the Axe of Might")
elif input(2):
    print("You chose the Sacred Crossbow")
else:
    print("You chose the Elven Sword")

I would expect the output to ask me for a number (1, 2, or 3) and then print the string that is associated with that number. Instead, when I input 1, it prints 1, then 2, and then the string associated with number 3 (the 'else' option), regardless of what number I type. I don't understand why?

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.1
1
2
You chose the Elven Sword

Process finished with exit code 0
1
  • What did you expect if input(1) to do other than prompt the user with a 1? Commented Sep 21, 2019 at 8:24

5 Answers 5

1

Try this:

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

if weapon_choice=='1':
    print("You chose the Axe of Might")
elif weapon_choice=='2':
    print("You chose the Sacred Crossbow")
else:
    print("You chose the Elven Sword")

Note :

  1. weapon_choice don't need to be cast into str format as input() will be already in string formatting.
  2. Whenever you do input(1) or input(2) it basically prompts user to give another input in stead of checking with the condition.

Output :

michael@arkistarvh:/$ python text_game.py 
You can choose between three weapons to defeat the beast! Press 1 for Axe, 2 for Crossbow, 3 for Sword.
3
You chose the Elven Sword
Sign up to request clarification or add additional context in comments.

1 Comment

I see! Thank you so much for explaining this! I was trying to refer to the 'input' the same way you do a list if that makes sense (thinking if the user input 1, then show this). I see the what I did wrong now.
1

You should have instead:

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

if weapon_choice == '1':
    print("You chose the Axe of Might")
elif weapon_choice == '2':
    print("You chose the Sacred Crossbow")
elif weapon_choice == '3':
    print("You chose the Elven Sword")
else:
    print("Invalid input selected")

The reason for this is that input(..) results in a string being parsed, so there is no need to have a str(..) around the input(..). Furthermore, you should have a condition where an invalid input is passed, so that the user will be notified more clearly of the root cause of the error.

(Note the \n which signifies the start of a new line)

5 Comments

Thank you! I will add the 4th option for invalid input, that's a great idea!
Hi @Stoner! I added the 'invalid input' option, but I want it to go back through the loop. I have tried 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.") continue but it doesn't work :(
I get Traceback (most recent call last): File "/Users/elissavetvelli/PycharmProjects/Giraffe/2dlist.py", line 10, in <module> weapon_choice <= '4' NameError: name 'weapon_choice' is not defined. I have tried lots of things, and at best I got it to show ```Wanderer, there is no such weapon. Please choose from the ones available`` infinitely!
@nephele you should have 'weapon_choice' outside of the loop. If you have it inside the loop, it will get "refreshed" during each iteration, hence the infinite loop. In any case, it is sort of a convention here in the community to have 1 question per problem, so it'll be best if you post further questions not related to the original problem as a new, separate question. People here may not check back to closed questions so your new question may get missed out. :')
Thank you! I will do that from now on - and thanks for taking the time to explain.
1

That's just not how input works in Python.

You correctly assumed that input("some text") would print you that text on the first line (and store the result in a variable weapon_choice), so why do you think that input(number) would return a boolean value telling you if the input was that number?

Instead, what it does is prints the number again and returns an empty string (because you presumably just pressed Enter), so the first two ifs are False and the program goes into else, printing "Invalid input selected".

The result of your first input will be stored in weapon_choice, so you should do the comparing on that variable.

Comments

0

if input(1) calls the input function with the argument 1, before using the response to test whether or not to execute the code in its block. Since executing input simply prints its argument to the terminal, and returns whatever the user inputs in response, your program is behaving exactly as your code implies.

Since you already stored the user's response to the first question in a variable called weapon_choice, you should replace if input(1) with if weapon_choice == '1' - and the same for the others.

Comments

0

you can try this script

weapon_choice = str(input("You can choose between three weapons to defeat the beast!"
                   " Press 1 for Axe, 2 for Crossbow, 3 for Sword."))
if weapon_choice == '1':
    print("You chose the Axe of Might")
elif weapon_choice == '2':
    print("You chose the Sacred Crossbow")
else:
    print("You chose the Elven Sword")

1 Comment

No, you can't. raw_input doesn't even exist in Python 3, and Python 2 is soon to be retired

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.