looking for advice python

Chris Angelico rosuav at gmail.com
Sat Jan 26 17:48:01 EST 2013


On Sun, Jan 27, 2013 at 9:26 AM,  <twiztidtrees at gmail.com> wrote:
> miles = int(string_miles)
> gas = int(string_gas)
>
> #used to calculate mpg through division
> mpg = miles/gas
>
> print(float(string_miles))
> print(float(string_gas))
> print('Your miles per gallon is', format(mpg,'.2f'))

Welcome aboard!

You turn your inputs into integers, then display them as floats...
from the original strings. (Though I guess this is probably debugging
code?) I would advise against doing this; at very least, it's
confusing. I would recommend simply using floats everywhere, and thus
allowing non-integer inputs:

How many miles did you drive?60.9
How many gallons of gas did you use?11.9
Traceback (most recent call last):
  File "123.py", line 11, in <module>
    miles = int(string_miles)
ValueError: invalid literal for int() with base 10: '60.9'

Small additional point: It's common to put a space at the end of your
prompt, to avoid the "crammed" look of "drive?60.9".

Are there any particular areas that you'd like comments on?

ChrisA



More information about the Python-list mailing list