2

So I got a piece of code that is providing me with some headache.

def Handy():
    print "\nMöchten Sie ein neues Handy?"
    print "\n1 - Ja\n2 - Nein"

    handy = raw_input(">>> ")

    if handy == "2":
        print "\nSie möchten kein neues Handy"
    elif handy == "1":
        wunschhandy = raw_input("\nBitte geben Sie den Namen des Handys ein: ")
    else:
        Handy()
    return handy, wunschhandy

If I choose option "1" everything is ok. But if I go for "2" it gives me this error: "UnboundLocalError: local variable 'wunschhandy' referenced before assignment"

I know why it is happening, or at least I think I know why. It is because the var wunschhandy has not been decleared, because I used option "2".

So how do I solve this problem? Is there a way to only return a value, if it has been assigned by the right if choice?

3
  • 2
    Do you really want to return either the string "2" or a 2-tuple containing "1" and something else? What does the caller do with the return values? Commented Mar 26, 2013 at 17:23
  • 1
    Not completely related to your question, but when you call Handy() in the else block, you don't do anything with the result. You probably want return Handy(). (Also, making the function repeat by calling itself will cause it to crash if the user enters incorrect input 500 times in a row. But that's not likely to be a practical issue.) Commented Mar 26, 2013 at 17:24
  • No, I do not want to return the function. I just want to run it agian. Commented Mar 26, 2013 at 17:34

5 Answers 5

1

In your case I would set wunschhandy = None before the if statement.

In this case you have a defined value, and the caller can test for it.

Another option would be

if handy == "2":
    print "\nSie möchten kein neues Handy"
    return handy,
elif handy == "1":
    wunschhandy = raw_input("\nBitte geben Sie den Namen des Handys ein: ")
    return handy, wunschhandy
else:
    Handy()

which returns a tuple of length 2 for option 1 and a tuple of length 1 for option 2. The caller can easily test that.

Besides, instead of calling Handy() recursively, I would put a loop inside it. Otherwise you might end in a stack overflow error...

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

Comments

1

If you want to return wunschhandy only if the user chooses "1", then place the return statements within the if blocks

def Handy():
    print "\nMöchten Sie ein neues Handy?"
    print "\n1 - Ja\n2 - Nein"

    handy = raw_input(">>> ")

    if handy == "2":
        print "\nSie möchten kein neues Handy"
        return handy
    elif handy == "1":
        wunschhandy = raw_input("\nBitte geben Sie den Namen des Handys ein: ")
        return handy, wunschhandy
    else:
        Handy()
        //do you want to return Handy()?

Comments

0

Just based on the small snippet of code you provided, it seems the 'wunschhandy' variable is only being defined if 'handy == "1"', but this variable is being returned at the end. I would set wunschhandy equal to null above the if statement so the variable is defined before trying to return it at the end.

Comments

0

It is because the variable wunschhandy is returned before it is declare. Only if you enter 1 it is declared. You may declare the wunschhandy before the if statements and assign it a defualt value.

Comments

0

Just initialise wunschhandy, so it's in the scope:

def Handy():
    handy = raw_input("Moechten Sie ein neues Handy?\n1 - Ja\n2 - Nein\n>>> ")

    wunschhandy =  None
    if handy is "2" :
        print "\nSie moechten kein neues Handy"
    elif handy is "1" :
        wunschhandy = raw_input("\nBitte geben sie den Namen des Handys ein: ")
    else :
        Handy()
    return handy, wunschhandy

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.