I have started programming with python yesterday, so I'm quite a newbie!
I have this function, which must check
- if an inserted value is a number
- if the number is no greater than 31 (see code below)
During debugging, I have found out this bug I don't understand:
- I choose deliberately a number greater than 31, for example 45
- it prompts me again and I choose a correct number, for example 7
- In the code, I ask to print the voto variable twice (in the comments I call them 'POINT A' and 'POINT B')
in the output I get:
7
45
and I'm again asked to type in a different number.
I don't understand why the variables changes its value just after the while loop has started.
Can you please explain it to me, using very simple words? (<- please, remember I'm a beginner! :D )
Thank you in advance!
def controlla_voto(voto_lett):
flag=1
while flag:
for y in voto_lett:
if (ord(y) in range(48,58))==0:
voto_lett=raw_input("Invalid charachters, try again: ")
flag=1
break
else: flag=0
voto=int(voto_lett)
print voto # POINT A
while (voto in range(32))==0:
print voto #POINT B
voto_lett=raw_input("Invalid number, try again: ")
controlla_voto(voto_lett)
return voto