Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37743
| Date | 2013-01-26 18:00 -0500 |
|---|---|
| From | Dave Angel <davea@davea.name> |
| Subject | Re: looking for advice python |
| References | <39d427b2-f5d7-4bd9-99f8-8cfd8d25b3cc@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1092.1359241236.2939.python-list@python.org> (permalink) |
On 01/26/2013 05:26 PM, twiztidtrees@gmail.com wrote:
> Hey I'm new to programming and I have been working on calculating miles per gallon. iv posted below what I have and constructive criticism would be wonderful. Thanks
>
A good post for the python-tutor mailing list.
If you want help with a program, the first thing you should specify is
what version of Python you're using. And usually which OS you're
running, but in this case it doesn't matter much.
I don't see either a shebang line nor a coding line.
> #This is a program used to calculate miles per gallon
>
>
> #variable used to gather miles driven
> string_miles = input('How many miles did you drive?')
>
> #variable used to gather the amount of gallons used
> string_gas = input('How many gallons of gas did you use?')
>
Why do you bother to have separate variables for the string versions?
Why not just miles = int( input("xxxx")) ? For that matter, what if
the user enters a decimal value for the gallons? Perhaps you'd better
use gas = float( input("yyy") )
> #used to convert the miles input
> miles = int(string_miles)
>
> #used to convert the gas input
> gas = int(string_gas)
>
> #used to calculate mpg through division
> mpg = miles/gas
This isn't portable to Python 2.x. In 2.x, it would truncate the result.
>
> print(float(string_miles))
> print(float(string_gas))
> print('Your miles per gallon is', format(mpg,'.2f'))
>
What if the user enters something that isn't a valid number, either int
or float? Where's the try/catch to handle it?
Is this a class assignment? If not, why would you have a comment and a
blank line between every line of useful code?
--
DaveA
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