Good Morning help simplify - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Good Morning help simplify (/thread-19856.html) |
Good Morning help simplify - raymond2688 - Jul-17-2019 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)) RE: Good Morning help simplify - ichabod801 - Jul-17-2019 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 * total1I would maybe rename those variables for clarity: gallons_used = distance / mpg total_cost = gallons_used * ppg RE: Good Morning help simplify - raymond2688 - Jul-17-2019 (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: Thank you very much for your input. I am starting to somewhat grasp this stuff...lol RE: Good Morning help simplify - jefsummers - Jul-22-2019 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. |