0

I just started learning python 2 days ago and Im trying to make some kind of text based adventure to practice , the only problem Im having is with functions:

def menu_op():
 print('1- Action 1')
 print('2- Action 2')
 choice= input('Choose action: ')
 return choice
def action_op(x):
 if x == 1:
 print('You chose action 1')
 if x == 2:
 print('You chose action 2')
menu_op()
action_op(menu_op())

The idea behind this is to call the menu function , which gives a value equal to user's input , which gets fed into the action function when the latter is called and does something depending on the user choice.

Cant tell what im doing wrong though , as the code doesnt seem to work. Thanks in advance

BenMorel
37.1k53 gold badges208 silver badges339 bronze badges
asked Aug 6, 2014 at 14:30
1
  • 2
    "doesnt [sic] seem to work" is not a helpful problem description. Errors (provide full traceback)? Unexpected outputs (provide inputs and expected and actual outputs)? Why do you call menu_op twice? Commented Aug 6, 2014 at 14:32

3 Answers 3

3

It looks like you are using Python 3.x. In that version, input returns a string object like raw_input did in Python 2.x. This means that the return value of the function menu_op will always be a string.

Because of this, you need to compare x with strings rather than integers:

if x == '1':
 print('You chose action 1')
elif x == '2':
 print('You chose action 2')

I also changed the second if to elif since x could never equal both '1' and '2'.

answered Aug 6, 2014 at 14:32
Sign up to request clarification or add additional context in comments.

3 Comments

This is indeed the correct answer thank you very much , I would have swore raw_input was used to return a string !! Thanks for the quick answer
raw_input does not exist in Python 3.
@Trenloco if you're used to Python 2.x, and you're now working in Python 3.x, you should absolutely take a look at the list of changes. Python 3.x is not backwards compatible by design. docs.python.org/3.0/whatsnew/3.0.html
1

You're calling menu_op() function twice. The first time it gets called choice is not passed to action_op()

menu_op() #return is not catched
action_op(menu_op())

And the value returned from menu_op is a string so you should compare strings in action_op instead of comparing x with integers

def action_op(x):
 if x == 1:
 ^^^ #should compare strings here -> x == "1"
answered Aug 6, 2014 at 14:34

Comments

0

choice= int(input('Choose action: ')) # make choice an int to compare

In [1]: 1 == "1"
Out[1]: False
In [2]: 1 == int("1")
Out[2]: True

input is a string and you are comparing if x == 1 where x is "1"

answered Aug 6, 2014 at 14:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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