Beginner trying to understand functions.

James Mills prologic at shortcircuit.net.au
Mon Dec 8 08:56:10 EST 2008


On Mon, Dec 8, 2008 at 11:32 PM, simonh <simonharrison.uk at googlemail.com> wrote:
> That works fine. Then I've tried to use functions instead. The first
> two work fine, the third fails:

[ ... snip ... ]

Try this:

def getName():
   name = input('Please enter your name: ')
   print('Hello', name)
   return name

def getAge():
   while True:
      try:
         return int(input('Please enter your age: '))
      except ValueError:
         print('That was not a valid number. Please try again.')

def checkAge(age):
      permitted = list(range(18, 31))
      if age in permitted:
         print('Come on in!')
         return True
      elif age < min(permitted):
         print('Sorry, too young.')
     elif age > max(permitted):
         print('Sorry, too old.')

      return False

name = getName()
age = getAge()
if checkAge(age):
   # Do something
else:
   # Do something else


cheers
James

PS: Read the Tutorial :)

-- 
--
-- "Problems are solved by method"



More information about the Python-list mailing list