Simple MPG calculator in Python

Course Queries Syllabus Queries . 2 years ago

  0   1   0   0   0 tuteeHUB earn credit +10 pts

5 Star Rating 5 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Write Your Comments or Explanations to Help Others



Tuteehub forum answer Answers (1)


profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago


I am a self taught coder taking a Programming Fundamentals class to work towards a degree. It's based on Python, which I'm not as familiar with as other languages. I added error handling like I would in javascript or php to be an overachiever. Professor gave online feedback (online course) not to use try-except, that it is ahead of where we are in the semester, that even when we do get there, she doesn't want it in our code, and that this code is "not well formed" because I used try-except. She gave no feedback as to what she meant and I don't think that sounds right but don't now enough to stand my ground. Googling "well formed python" just give me a bunch of xml.

# main function
def main():

    # @input dist : distance traveled in miles

    # @input guse : gas used traveling dist

    # @output mpg : dist/guse 

    # set dist from input "Enter miles traveled: "
    dist = input('Enter distance traveled(in miles): ')
    # make sure dist is a number, and if not print error and restart
    try:
        dist = float(dist)
    except ValueError:
        print('Distance traveled must be a number! You did not travel "' + \
              dist + '" miles!')
        main()
    else:
        # if dist is 0.0, print error and restart
        try:
            x = 1 / dist
        except ZeroDivisionError:
            print('Miles traveled cannot be 0!')
            main()
        else:
            # set guse from input "Enter gas used in gallons: "
            gcon = input('Enter gas consumed traveling(in gallons): ')

            # make sure guse is a number, and if not print error and restart
            try:
                gcon = float(gcon)
            except ValueError:
                print('Gas consumed must be a number. You did not use "' + \
                          gcon + '"" gallons of gas!')
                main()
            else:

                # set mpg to dist divided by guse
                # if guse is 0.0, print error and restart
                try:
                    mpg = dist / gcon
                except ZeroDivisionError:
                    print('Gas consumed can not be 0!')
                    main()
                else:

                    # return(print) mpg
                    print('Average miles per gallon(mpg):', format(mpg, '5.2f'))

                    # print request for enter keypress to end
                    input('press enter to continue')

# Call main function
main()
0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community