looking for advice python

Dave Angel davea at davea.name
Sat Jan 26 18:00:10 EST 2013


On 01/26/2013 05:26 PM, twiztidtrees at 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



More information about the Python-list mailing list