Traceback (most recent call last):

Josiah Carlson jcarlson at nospam.uci.edu
Tue Mar 23 21:47:40 EST 2004


> # If no arguments were given, print a helpful message
> if len(sys.argv)==1:
>     print 'Usage: celsius temp1 temp2 ...'
>     sys.exit(0)
> 
> # Loop over the arguments
> for i in sys.argv[1:]:
>     try: 
>         fahrenheit=float(string.atoi(i))

You should try the below instead, then you don't need string.

           farenheit = float(i)

If someone were to pass in an integer with more than 309 significant 
digits (and you kept the original float(string.atoi(i)) ), you would 
receive:
OverflowError: long int too large to convert to float

which wouldn't be handled by the subsequent except clause.

>     except string.atoi_error:
>       print repr(i), "not a numeric value"

Just because string fails to convert a value to an integer, doesn't mean 
that the value isn't numeric.  It could very well have been a number 
with a decimal point (which would raise ValueError), or even (as 
previously mentioned), too large.

>     else:
>       celsius=(fahrenheit-32)*5.0/9.0

Farenheit is already a float, so math with it will proceed with floating 
point values, no need for "*5.0/9.0", "*5/9" is sufficient.

>       print '%i\260F = %i\260C' % (int(fahrenheit), int(celsius+.5))

Why are you converting these floats to integers?  Is that part of the 
spec of the output?  Why are you adding .5 to celcius, round(val, 
digits) would work better.

> After I type in the above code in new window save as temperature.py
> when I click Run Module, Python show me the following error message
> 
> Usage: celsius temp1 temp2 ...
> 
> Traceback (most recent call last):
>   File "C:/Python23/Temperature.py", line 15, in -toplevel-
>     sys.exit(0)
> SystemExit: 0
> 
> Anyone have idea how to solve this up... I am using python 2.3(IDLE)
> (Python GUI)

Running a module in idle does not allow you to pass in parameters, and 
Idle captures sys.exit calls so that you don't accidentally kill idle 
with unsaved documents open.  Open up a command prompt and do the following:

C:\>cd \Python23
C:\Python23>python Temperature.py temp1 temp2 temp3

It should run as you expect.

  - Josiah



More information about the Python-list mailing list