[Tutor] calling and returning functions.
Dave Angel
davea at davea.name
Mon Feb 11 21:20:05 CET 2013
On 02/11/2013 03:07 PM, Pravya Reddy wrote:
> Can you please complete the code.
>
> #!/usr/bin/env python
> """ One function receives a value in inches and returns the equivalent
> value in
> cms like cm = 2.54 * in.The other function receives a value in cms and
> returns
> the equivalent value in inches like in = cm / 2.54."""
>
> def conversion(inch,cm):
> """returns the value in cm and in."""
> return (2.54 * float(inches))
> return (float(cm) / 2.54)
This second line does nothing, since the first line always returns.
But it doesn't matter much, since you never call it. Don't you want two
functions?
> def GetInt(prompt):
> """Returns a number, or None if user doesn't answer."""
> while True:
> said = input(input(prompt))
This has been corrected several times, by various people. What do you
think it does, and what do you wish it would do? Is there ever a reason
to call input with the results of a call to input?
> if not said:
> return None
> try:
> number = int(said)
> except ValueError:
> print (said, "is not a number.")
> continue
> return number
The return never gets executed, since it follows an unconditional
continue statement. Do you perhaps want it dedented?
> def Test():
> first = GetInt('Please enter inches:')
> if first:
> second = GetInt('Please enter cms:')
Do you always want to do a pair of conversions? if not, why are you
asking for both values before doing anything?
> print(first, "*", 2.54, "=", "cms")
This prints an equation without actually printing a result.
> print(second, "/", 2.54, "=", "in")
Ditto.
> Test()
>
--
DaveA
More information about the Tutor
mailing list