[Tutor] ValueError
Corey Richardson
kb1pkl at aim.com
Tue May 3 13:00:23 CEST 2011
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 05/03/2011 06:30 AM, Johnson Tran wrote:
> Hi All,
>
> i am trying to create a program module with two functions (conversion inches to centimeters then centimeter to inches, I have my program working although I am trying to adda Value Error function to my program but cannot seem to it to work:
>
>
> def Conversion():
> print "This program converts the first value from inches to centimeters and second value centimeters to inches."
> print "(1 inch = 2.54 centimeters)"
> inches = input("Enter length in inches: ")
> centimeters = 2.54 * inches
> print "That is", centimeters, "centimeters."
>
> centimeters = input("Enter length in centimeters: ")
> inch = centimeters / 2.54
> print "That is", inch, "inches."
>
> except ValueError:
> print "Invalid digit, please try again."
>
> Conversion()
>
>
>
> Any advice would be great, thanks!
>
> JT
Well, you need a 'try' block before that except. Example:
try:
foo = int("blargh")
except ValueError:
pass
It looks like you're forgetting an important operation on the inches
variable, as well as centimeters later on.
Take a look at
http://docs.python.org/library/stdtypes.html#typesnumeric
For future questions, it's best to include what your program /is/ doing
that you think it shouldn't be, as well as any errors (tracebacks, copy
the whole thing!) you get.
- --
Corey Richardson
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJNv+BHAAoJEAFAbo/KNFvpCdwH/0DRxVjevNxZy2HtYuxykzlA
x2ni1VnDMyS2YCsHvqIaglfK2hBeL+nstGL8kmEhGu4t5Z85nqGt9Ea2spPhPDxE
UJJ1O2nYFtLUZ1BC03vkC8aHI0aiijZjg7v7adKW4sD2laGTaeryLLR1qbGh3ZBP
rKTWK/NuyyMDRYjnP0gXsiYYNPc6E6WsbBTYWxGcMPwLnlvgmmXJBOLC6qj07eXu
X/fd5FwKSRJPMYIGT47zsnFdZdrN1SOOM537XH8CX+xQPTg/J8NeaiqoaWjTKLMn
PpXizt1AbuV1/0/Zqp6VKgTA/sxYtMfc4mFWjfovHlxJ/ahA19DaQjeWneIjldk=
=LkuV
-----END PGP SIGNATURE-----
More information about the Tutor
mailing list