Question
Python: I have two programs here, in the first section values are entered manually and in the second part read from a file. There should
Python: I have two programs here, in the first section values are entered manually and in the second part read from a file. There should be only one program which asks the user how the data is to be entered. Depending on the user's response, the appropriate function will be called.After this, you also call the other function (which has not been called as yet), and show that it works as well, the user should not call the same function twice. Please comment on the code sections and their function please.
def main():
print("Calculator of Fuel Efficiency")
odom1 = eval(input("Please enter the beginning odometer display: "))
miles_list = []
gas_list = []
user_in = input("Enter the odometer display and the gas used, seperated by a " + "space: ")
while user_in != "":
odom2, gas = uder_in.split()
odom2, gas = eval(odom2), eval(gas)
miles_list.append(odom2 - odom1)
gas_list.append(gas)
odom1 = odom2
user_in = input("Enter the odometer display and gas used, seperated " + "by a space: ")
print(" Leg MPG")
for i in range(len(miles_list)):
mpg = miles_list[i] / gas_list[i]
print("{0:2d} {1:13.1f}".format(i +1, mpg))
total_miles = sum(miles_list)
total_gas = sum(gas_list)
print("Total MPG: {0:5.1f}".format(total_miles / total_gas))
file_obj = open('trip.txt', 'r')
miles_list = []
gas_list = []
odom1 = eval(file_obj.readline())
for line in file_obj:
odom2, gas = line.split()
odom2 = eval(odom2)
gas = eval(gas)
mile = odom2 - odom1
miles_list.append(gas)
odom1 = odom2
file_obj.close()
print(" Leg MPG")
for i in range(len(miles_list)):
mpg = miles_list[i] / gas_list[i]
print("{0:2d} {1:13.1f}".format(i + 1, mpg))
total_miles = sum(miles_list)
total_gas = sum(gas_list)
print("Total MPG: {0:5.1f}".format(total_miles / total_gas))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started