[Tutor] Recursive user input collection problem

Alan Gauld alan.gauld at btinternet.com
Wed Oct 14 01:53:29 CEST 2009


"William Witteman" <yam at nerd.cx> wrote

> I want to make sure that I actually get integers.

> num_of_articles = 0
> num_of_reviewers = 0
>
> def getinput(variable,prompt):
>  """
>  Get the input by prompting the user and collecting the response - if it 
> is
>  a non-integer, try again.
>  """
>  variable = 0
>  variable = raw_input(prompt)
>
>  try:
>    int(variable)
>    return variable

The normal way in Python would be to combine this with

try:
    return int(raw_input(....))
except ValueError:
     blah...

>  except ValueError:
>    print("We need an integer (number) here.")
>    getinput(variable,prompt)

You don;t return the value from getinput().
Just add a return in front of the call...

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list