Python Forum
Good Morning help simplify
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Good Morning help simplify
#1
I was to create a program so I can compare the cost of fuel per vehicle I was wanting to purchase. I was able to get it to work but I think it can be simplified, maybe to clunky

# introductory statement
intro= '''
Automobile Fuel Cost Calculator

This program may help you decide which car makes sense for you
based on your budget. You will be asked to enter the MPG for the car
you have in mind, the average number of miles you expect to drive each
month and the cost per gallon for the fuel.
'''
print(intro)
# user adds miles per gallon
mpg = input("Please enter mpg (miles per gallon:")
mpg = float(mpg)
# user enters miles for the month
distance = input("Please enter the average miles driven in a month:")
distance = float(distance)
# user enters price per gallon
ppg = input("Enter fuel price per gallons:")
ppg = float(ppg)

a = distance
b = ppg
c = mpg

total1 = a/c
total2 = b*total1
print("Given that the price of fuel is at ${:.2f}/gallon and you travel around {} miles per month:".format(ppg, distance))

print("Amount of gallons in fuel used each month is: {0:.1f}".format(total1))
print("Your monthly estimated cost of fuel is: ${0:.2f}".format(total2))
Reply
#2
The main this I see is that you don't need a, b, or c. You could just as easily calculate with the original values:

total1 = distance / mpg
total2 = ppg * total1
I would maybe rename those variables for clarity:

gallons_used = distance / mpg
total_cost = gallons_used * ppg
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jul-17-2019, 02:01 PM)ichabod801 Wrote: The main this I see is that you don't need a, b, or c. You could just as easily calculate with the original values:

total1 = distance / mpg
total2 = ppg * total1
I would maybe rename those variables for clarity:

gallons_used = distance / mpg
total_cost = gallons_used * ppg

Thank you very much for your input. I am starting to somewhat grasp this stuff...lol
Reply
#4
You also can chain together functions. This can be overdone which detracts from readability, but
mpg = float(input("Please enter mpg (miles per gallon:"))
remains readable and drops a line of code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simplify a program using functions nzieno 8 9,764 Oct-06-2016, 07:29 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020