Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37742
| References | <39d427b2-f5d7-4bd9-99f8-8cfd8d25b3cc@googlegroups.com> |
|---|---|
| Date | 2013-01-27 09:48 +1100 |
| Subject | Re: looking for advice python |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1091.1359240489.2939.python-list@python.org> (permalink) |
On Sun, Jan 27, 2013 at 9:26 AM, <twiztidtrees@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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
looking for advice python twiztidtrees@gmail.com - 2013-01-26 14:26 -0800
Re: looking for advice python Chris Angelico <rosuav@gmail.com> - 2013-01-27 09:48 +1100
Re: looking for advice python Dave Angel <davea@davea.name> - 2013-01-26 18:00 -0500
Re: looking for advice python twiztidtrees@gmail.com - 2013-01-27 10:57 -0800
Re: looking for advice python twiztidtrees@gmail.com - 2013-01-28 13:59 -0800
csiph-web