Beginner trying to understand functions.

James Mills prologic at
Mon Dec 8 14:56:10 CET 2008

On Mon, Dec 8, 2008 at 11:32 PM, simonh < at> 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:
         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
   # Do something else


PS: Read the Tutorial :)

-- "Problems are solved by method"

More information about the Python-list mailing list