[Tutor] ValueError
Johnson Tran
aznjonn at me.com
Tue May 3 13:27:27 CEST 2011
Thanks for the replies..so I added the "try" block but it still does not seem to be outputting my default error message:
def Conversion():
try:
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 * float(inches)
print "That is", centimeters, "centimeters."
centimeters = input("Enter length in centimeters: ")
inch = float(centimeters) / 2.54
print "That is", inch, "inches."
except ValueError:
print "Invalid digit, please try again."
Conversion()
Error message:
Traceback (most recent call last):
File "/Users/JT/Desktop/hw#2.py", line 16, in <module>
Conversion()
File "/Users/JT/Desktop/hw#2.py", line 9, in Conversion
centimeters = input("Enter length in centimeters: ")
File "<string>", line 1, in <module>
NameError: name 'fs' is not defined
On May 3, 2011, at 4:00 AM, Corey Richardson wrote:
> -----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-----
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list