looking for advice python

T

twiztidtrees

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'))
 
C

Chris Angelico

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
 
D

Dave Angel

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?
 
T

twiztidtrees

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
 
T

twiztidtrees

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top