2

How do I make this work? I'm trying to set a global function so that I can simply call it later for raw input rather than calling the raw input where I need it.

I think I have the essence to an extent of what I need but I'm stumped as to how to format this, or if it's even possible.

Thank you in advance.

   def choice_1():
        choice_1 == raw_input("> ")
    def choice_1a():    
        choice_1a = raw_input("> ") 
    def choice_1b():    
        choice_1b = raw_input("> ")

edit: I don't think I was clear enough in the purpose of my question. Here's an update of the code I'm working on, perhaps this will clear things up.

print "You've arrived at your desk"


def choice_1(one):
    choice_1a = raw_input("< ")
def choice_1a():    
    choice_1a = raw_input("> ") 
def choice_1b():    
    choice_1b = raw_input("> ")

#Choice_1
print "What do you want to do?"
print "We can \n1. Read\n2. Draw\n3. Work on homework"
print choice_1
#choice 1 branch 1
if choice_1 == "1":
    print "What book should we read today?"
    print "We can read\n1. Tom Sawyer\n2. Quantum Physics \n3. Ray Bradbury"
    print choice_1a
    if choice_1a == "1":
        print "Great choice!"
    if choice_1a == "2":
        print "Heavy stuff there."
    if choice_1a == "3":
        print "Entertaining author, that one there!"
    else:
        print "Let's go to the library, maybe they'll have that one."
#choice 1 branch 2
if choice_1 == "2":
    print "What would you like to draw?"
    print "We can draw a\n1. Tiger\n2. Fish\n3. Bear "
    print choice_1b
    if choice_1b == "1":
        print "You drew a Tiger!"
    if choice_1b == "2":
        print "You drew a Fish!"
    if choice_1b == "3":
        print "You drew a Bear!"
    else:
        print "Time for some improvisation."

#choice 1 branch 3
if choice_1 == "3":
    print ""

does this clear some confusion?

1
  • 1
    Looks like all you are missing is some return statements. Commented Jun 27, 2015 at 3:23

2 Answers 2

1

This will do what you want. As @SethMMorton had noted, you had missed out on a return

def choice():
    return raw_input('> ‘)

By the way, this is not a nice thing to do, because, to someone reading your code, it will not be immediately clear what choice is doing.

I did this:

print 'Your name is :' + choice()

And it worked as expected.

EDIT: You should make your if statements thus:

if choice() == "1":

Sign up to request clarification or add additional context in comments.

8 Comments

this does not work, returns not an error code, but it's calling the function at the function, if I'm saying that right.
@Ppaisley Hm, how exactly are you calling this?
I need to be able to call the raw_input first, but thank you for the constructive comment
@Ppaisle When you write something like choice_1a == '1' (as you have done) you are not comparing the return value of the function to 1, you are comparing the function itself to 1, which is certainly not correct.
you made it work! thank you very much Rishav, you've been a great help. I understand what you're saying about the comparison. so what was missing is telling python to compare the raw_input value to the function?
|
0
 def multiplier(x, y):
     ans = x * y
     print ans

This would be a use for a function and when ready to use it you would call it like this

 multiplier(5, 10)
      50

13 Comments

I'm trying to be able to call "choice_1" and have it ask for a raw input at that point in the code, so I can "set up" my functions at the beginning of the code, not at each point that I want them. edit: perhaps I don't grasp the purpose of def properly??
I see alright give me ten mins let me see what i can come up with
Er This takes two lines of input, and returns the second one. This isn’t correct, I’m afraid.
yea your not to clear on functions they can't be called like that first of all you should just add your raw_input() functions where need be its pretty much the same thing.
yes you are correct lol thats why i love Python so many styles and ways to write the code its wonderful.
|

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.