[Tutor] calling and returning functions.

Alan Gauld alan.gauld at btinternet.com
Tue Feb 12 00:21:53 CET 2013

On 11/02/13 20:07, Pravya Reddy wrote:
> def conversion(inch,cm):
>      """returns the value in cm and in."""
>      return (2.54 * float(inches))
>      return (float(cm) / 2.54)

return exits the function so the second return statement never gets 

> def GetInt(prompt):
>      """Returns a number, or None if user doesn't answer."""
>      while True:
>          said = input(input(prompt))

You are still using double input. That is almost never the
right thing to do.

>          if not said:
>              return None
>          try:
>              number = int(said)
>          except ValueError:
>                  print (said, "is not a number.")
>                  continue
>                  return number

The continue will jump right back to the loop start so the return will 
never be executed.

The only way this function will ever exit is if the user hits Enter 
twice and then it will return None.

You need to rethink the logic here.

> def Test():
>      first = GetInt('Please enter inches:')
>      if first:
>          second = GetInt('Please enter cms:')
>          print(first, "*", 2.54, "=", "cms")
>          print(second, "/", 2.54, "=", "in")

Since GetInt() can only ever return None the if will always fail and 
nothing will ever be printed.

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list