3

I have started programming with python yesterday, so I'm quite a newbie!

I have this function, which must check

  1. if an inserted value is a number
  2. if the number is no greater than 31 (see code below)

During debugging, I have found out this bug I don't understand:

  1. I choose deliberately a number greater than 31, for example 45
  2. it prompts me again and I choose a correct number, for example 7
  3. In the code, I ask to print the voto variable twice (in the comments I call them 'POINT A' and 'POINT B')
  4. 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
11
  • 2
    (ord(y) in range(48,58))==0? Did you mean not isdigit(y)? Commented Aug 9, 2013 at 20:35
  • 2
    Fix the indentation in the pasted program; otherwise we can't understand it. Commented Aug 9, 2013 at 20:39
  • I am trying to help you, but the code is unindented, and I can`t figure out how do you call this method... Commented Aug 9, 2013 at 20:40
  • @F3AR3DLEGEND Oh! it's a recursive function! Commented Aug 9, 2013 at 20:42
  • 1
    I have fixed the indentation, sorry for that! Commented Aug 9, 2013 at 20:47

1 Answer 1

2

It's perfect! You just forgot the return on the recursive call.

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: ")
    return controlla_voto(voto_lett)
return voto

Another solution would be:

voto = controlla_voto(voto_lett)

but something is needed to break out of the while loop.

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

13 Comments

@user2669155 because you'll have to return whatever the call gave you. If you don't, you'll either return None (if you don't hit a return statement) or return something different which might be an old value.
Thanks Jim for voting the question! I will certainly vote the answer, just please keep on explaining until I get it... :-$
OK, here's what's going on: The integer voto NEVER changes in the lower while loop, so the only way to get out is to have a return. Without a return, controlla_voto (the second one) just finishes and you are stuck back in that loop. With voto still being something over 31.
A new copy of voto is created everytime you call the function. So when the function ends (without a return) then that context ends and the old value is back at that level of processing. Recursion is tricky, and it is different from copying and pasting the code, because call has a new context.
@Antonis Christofides: yours is not exactly a costructive comment :D It's true that I'm a beginner, but either you give me a list of what I should clear up first or it's not going to help. I use a recursive function because I need a recursive function.
|

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.