[Tutor] help with inch to cms conversion .
Dave Angel
davea at davea.name
Mon Feb 11 17:25:01 CET 2013
On 02/11/2013 11:06 AM, Pravya Reddy wrote:
> Can you please help me with the code.
>
> #!/usr/bin/env python
> """
> inchtocm.py
>
> """
First, remove that try/except until the code is free of obvious bugs.
It's masking where the error actually occurs. Alternatively, include a
variable there, and print the stack trace yourself.
>
> def Inchtocm(inches):
> """Returns 2.54 * inches"""
> return (2.54 * float(inches_number1))
As Joel pointed out, you're using a global instead of the parameter that
was passed. Call float() on inches, not on some non-local variable.
>
> inches = None
> while True:
> try:
> inches_number1 = input(input("How many inches you want to convert:
> "))
Calling input(input()) doesn't do any favors. It echoes the first
response, and waits for another one. The user doesn't probably realize
that he has to type the number 455 twice.
> inches = float(inches_number1)
Since you're presumably getting the exception on this line, you should
print out the value you're trying to convert. You can remove the print
after it works.
> print ("You got", Inchtocm(inches), "cm.")
> print ("You converted", inches, "inches to cm.")
> break
> except ValueError:
> print ("This is not a number!")
>
> The code is incomplete and i am not getting a proper output:
>
> How many inches you want to convert: 455
> 455
> This is not a number!
> How many inches you want to convert:
>
--
DaveA
More information about the Tutor
mailing list