Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)

 twiztidtrees@gmail.com 01-26-2013 10:26 PM

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

#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?')

#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

print(float(string_miles))
print(float(string_gas))
print('Your miles per gallon is', format(mpg,'.2f'))

 Chris Angelico 01-26-2013 10:48 PM

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

 Dave Angel 01-26-2013 11:00 PM

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

 twiztidtrees@gmail.com 01-27-2013 06:57 PM

I am in a class and was just looking for different advice. This is the first time iv ever tried to do this. That's all that iv taken from two chapters and wondering how bad I did. I also like to learn. Thanks for everyones input

 twiztidtrees@gmail.com 01-28-2013 09:59 PM

On Sunday, January 27, 2013 1:57:47 PM UTC-5, twizti...@gmail.com wrote:
> I am in a class and was just looking for different advice. This is the first time iv ever tried to do this. That's all that iv taken from two chapters and wondering how bad I did. I also like to learn. Thanks for everyones input

This is what I have now thanks for the advice. It did seem a lot easier this way, but any criticism would be nice thanks.

windows 7 and python 3.3.0

while True:
try:
miles = float(input("How many miles did you drive?"))
break
except ValueError:
print("That is not a valid number. Please try again.")
while True:
try:
gas = float(input("How many gallons of gas did you use?"))
break
except ValueError:
print("That is not a valid number. Please try again.")

mpg = miles/gas
print('Your miles per gallons is', format(mpg,'.2f'))

 All times are GMT. The time now is 06:11 AM.