[Tutor] Recursive user input collection problem

Weidner, Ronald RWeidner at ea.com
Tue Oct 13 22:52:32 CEST 2009


I didn't test this but shouldn't you have a line like this...

return getinput(variable,prompt)

--
Ronald Weidner

-----Original Message-----
From: tutor-bounces+rweidner=ea.com at python.org [mailto:tutor-bounces+rweidner=ea.com at python.org] On Behalf Of William Witteman
Sent: Tuesday, October 13, 2009 4:22 PM
To: tutor at python.org
Subject: [Tutor] Recursive user input collection problem

I need to collect a couple of integers from a user, but I want to make
sure that I actually get integers.  I tried this, but subsequent calls
to the function don't update variable.  I'm not sure this is terribly
clear - here's the code:

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

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


num_of_articles = getinput(num_of_articles,"Enter number of articles: ")
num_of_reviewers = getinput(num_of_reviewers,"Enter number of reviewers: ")

print(num_of_articles)
print(num_of_reviewers)


This works fine if I put in good input, but not if I pass in a bad
value.  Can anyone show me where I have gone astray?  Thanks.
-- 

yours,

William

_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list