I just started programming and tried to write something but (of course) it failed. After I while I got to the real problem: The UnboundLocalError. So to save you from all the rubble around I stripped the code down to this:
def test():
try:
i1 = int(i1)
i2 = int(i2)
except ValueError:
print "you failed in typing a number"
def input():
i1 = raw_input('please type a number \n >')
i2 = raw_input('please type a number \n >')
Then I wrote down:
>>>input()
please insert a number
> 3
please insert a number
> 2
>>>test()
And then I got:
that was not a number
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in test
UnboundLocalError: local variable 'i1' referenced before assignment
How can I solve this in a Pythonic way? Or should I take a whole different way?
The most standard way to do this is to give parameters to your test method :
If you really want to test on the interactive prompt, you can do (as suggested in @FerdinandBeyer comment) :
And then, on prompt :