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?
Handy()in theelseblock, you don't do anything with the result. You probably wantreturn 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.)