Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37740 > unrolled thread

looking for advice python

Started bytwiztidtrees@gmail.com
First post2013-01-26 14:26 -0800
Last post2013-01-28 13:59 -0800
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#37740 — looking for advice python

Fromtwiztidtrees@gmail.com
Date2013-01-26 14:26 -0800
Subjectlooking for advice python
Message-ID<39d427b2-f5d7-4bd9-99f8-8cfd8d25b3cc@googlegroups.com>
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'))

[toc] | [next] | [standalone]


#37742

FromChris Angelico <rosuav@gmail.com>
Date2013-01-27 09:48 +1100
Message-ID<mailman.1091.1359240489.2939.python-list@python.org>
In reply to#37740
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

[toc] | [prev] | [next] | [standalone]


#37743

FromDave Angel <davea@davea.name>
Date2013-01-26 18:00 -0500
Message-ID<mailman.1092.1359241236.2939.python-list@python.org>
In reply to#37740
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

[toc] | [prev] | [next] | [standalone]


#37784

Fromtwiztidtrees@gmail.com
Date2013-01-27 10:57 -0800
Message-ID<6cc43b88-daf0-40fa-ae29-8fda465e08ce@googlegroups.com>
In reply to#37743
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

[toc] | [prev] | [next] | [standalone]


#37842

Fromtwiztidtrees@gmail.com
Date2013-01-28 13:59 -0800
Message-ID<317d381c-25ac-47c8-a421-7b6d501c986a@googlegroups.com>
In reply to#37784
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'))

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web