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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!