looking for advice python

Discussion in 'Python' started by twiztidtrees@gmail.com, Jan 26, 2013.

  1. Guest

    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'))
    , Jan 26, 2013
    #1
    1. Advertising

  2. On Sun, Jan 27, 2013 at 9:26 AM, <> 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
    Chris Angelico, Jan 26, 2013
    #2
    1. Advertising

  3. Dave Angel Guest

    On 01/26/2013 05:26 PM, 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
    Dave Angel, Jan 26, 2013
    #3
  4. Guest

    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
    , Jan 27, 2013
    #4
  5. Guest

    On Sunday, January 27, 2013 1:57:47 PM UTC-5, 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'))
    , Jan 28, 2013
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Asun Friere
    Replies:
    1
    Views:
    484
    Paul Boddie
    Aug 27, 2003
  2. Peter Hansen
    Replies:
    23
    Views:
    840
    Anton Vredegoor
    Sep 5, 2003
  3. Gerrit Holl
    Replies:
    16
    Views:
    602
    Tom Plunket
    Aug 29, 2003
  4. Asun Friere
    Replies:
    0
    Views:
    468
    Asun Friere
    Aug 28, 2003
  5. Steven W. Orr
    Replies:
    4
    Views:
    299
    Carl Banks
    Apr 1, 2008
Loading...

Share This Page