You previously wrote a program that calculates and displays:
Miles driven
Miles per gallon
Kilometers driven
Liters of fuel consumed
Kilometers per liter
Messages commenting on a vehicle's fuel efficiency
And also validates input
Now we'll make a somewhat major change. We'll remove the code for obtaining input from a user and replace it with hardcoded data stored in lists. Because the data is hardcoded, the program will no longer need the code for validating input. So, you may remove that, as well.
Since the program needs three lists representing the items of input – mileage at beginning of the measurement period, mileage at end of the period and gallons of fuel consumed – we'll create three lists – one for beginning mileage, a second for ending mileage and a third for gallons consumed – and hardcode the data in them. By iterating through a loop which has statements for retrieving elements from these lists, the program will obtain the input it needs for performing calculations and producing output. To make this easy, let's use these lists:
beginning_miles_list = [ 100, 200, 300, 400, 500, 600 ]
ending_miles_list = [ 400, 500, 600, 700, 800, 900 ]
gallons_used_list = [ 25.0, 18.75, 13.64, 10.71, 9.38, 8.33 ]
If you feel daring and innovative, use a two-dimensional list:
fuel_usage_list = [] # just a plain empty list
fuel_usage_list.append( beginning_miles_list )
fuel_usage_list.append( ending_miles_list )
fuel_usage_list.append( gallons_used_list )
See the attached program Fuel_Efficiency_2D.py for more on this.
Create the lists and load data into them before the loop.
Except for simplified academic situations, like this one, we almost never Hardcode data in programs. Fear not, later we'll obtain data more realistically by reading it from a file.
Language: PYTHON
Attached file with the question:
# create a list for each set of data values
beginning_miles_list = [ 100, 200, 300, 400, 500, 600 ]
ending_miles_list = [ 400, 500, 600, 700, 800, 900 ]
gallons_used_list = [ 25.0, 18.75, 13.64, 10.71, 9.38, 8.33 ]
# create an empty list to hold the 3 previous lists
fuel_usage_list = []
fuel_usage_list.append( beginning_miles_list ) # append the beginning miles list to fuel_usage_list.
fuel_usage_list.append( ending_miles_list ) # append the ending miles list to fuel_usage_list.
fuel_usage_list.append( gallons_used_list ) # append the gallons used list to fuel_usage_list.
# now fuel_usage_list is a list of 3 elements, or rows, each containing a list of 6 elements, which are the columns
print("\n\n")
for row_index in range( len( fuel_usage_list ) ):
for column_index in range( len( fuel_usage_list[ row_index ] ) ):
print( fuel_usage_list[ row_index ][ column_index ], "\t", end="") # end="" prevents newline from being wriiten
print("\n") # print a new line here to delineate rows
My actual code:
name = input("\nHello Dear customer, please enter your username : ")
def take_input(s):
value = 0
while True:
try:
value = float(input(s))
if value <= 0:
print("Enter a valid positive numeric value")
value = take_input(s)
break
except ValueError:
print("Enter a valid positive numeric value")
return value
MB = take_input("\nPlease enter the mileage at Mileage at
beginning of measurement period: ")
MEP = take_input("\nPlease enter the Mileage at end of measurement
period: ")
GFC = take_input("\nPlease enter the Gallons of fuel consumed: ")
print("\nThank you! please wait...")
print("\nHere are your results:")
MilesDrive = (MEP - MB)
MPG = (MilesDrive / GFC)
KM = (1.60934 * MilesDrive)
FC = (GFC * 3.785412)
KML = (KM / FC)
print("\nYou drived: ", + MilesDrive,)
print("\nYou consumed: ", + MPG, "MPG")
print ("\nYou drived: ", + KM, "kilometrs",)
print("\nDuring your trip you consumed: ", + FC, "liters")
print("\nDuring your trip you consumed: ", + KML, "Liters per Kilometer")
if MPG < 15:
print("\nYour vehicle has criminally low fuel efficiency.")
elif MPG >= 15 and MPG < 20 :
print( "\nYour vehicle has undesirably low fuel efficiency.")
elif MPG >= 20 and MPG < 25 :
print("\nYour vehicle gets barely acceptable efficiency.")
elif MPG >= 25 and MPG < 35 :
print( "\nYour vehicle gets high fuel efficiency.")
else:
print("\nYour vehicle gets outstanding fuel efficiency.")
print("\nThank you for visit our website, have a great date, " +
name, "!")
beginning_miles_list = [100, 200, 300, 400, 500, 600] ending_miles_list = [400, 500, 600, 700, 800, 900] gallons_used_list = [25.0, 18.75, 13.64, 10.71, 9.38, 8.33] fuel_usage_list = [] fuel_usage_list.append(beginning_miles_list) # append the beginning miles list to fuel_usage_list. fuel_usage_list.append(ending_miles_list) # append the ending miles list to fuel_usage_list. fuel_usage_list.append(gallons_used_list) # append the gallons used list to fuel_usage_list. # now fuel_usage_list is a list of 3 elements, or rows, each containing a list of 6 elements, which are the columns for row_index in range(len(fuel_usage_list)): for column_index in range(len(fuel_usage_list[row_index])): print(fuel_usage_list[row_index][column_index], "\t", end="") # end="" prevents newline from being written print() # print a new line here to delineate rows row_index = 0 for column_index in range(len(fuel_usage_list[row_index])): # Calculate using the formulas you used before # Extract mileage in beginning, ending and gallons from fuel_usage_list MilesDrive = (fuel_usage_list[row_index + 1][column_index] - fuel_usage_list[row_index][column_index]) MPG = (MilesDrive / fuel_usage_list[row_index + 2][column_index]) KM = (1.60934 * MilesDrive) FC = (fuel_usage_list[row_index + 2][column_index] * 3.785412) KML = (KM / FC) print("\nHere are your results:") print("You drived: ", + MilesDrive, ) print("You consumed: ", + MPG, "MPG") print("You drived: ", + KM, "kilometers", ) print("During your trip you consumed: ", + FC, "liters") print("During your trip you consumed: ", + KML, "Liters per Kilometer") if MPG < 15: print("\nYour vehicle has criminally low fuel efficiency.") elif MPG >= 15 and MPG < 20: print("\nYour vehicle has undesirably low fuel efficiency.") elif MPG >= 20 and MPG < 25: print("\nYour vehicle gets barely acceptable efficiency.") elif MPG >= 25 and MPG < 35: print("\nYour vehicle gets high fuel efficiency.") else: print("\nYour vehicle gets outstanding fuel efficiency.") print()
SCREENSHOT
OUTPUT
You previously wrote a program that calculates and displays: Miles driven Miles per gallon Kilometers driven...
Write a Python program that does the following: Obtains the following input from a user: Mileage at beginning of measurement period Mileage at end of measurement period Gallons of fuel consumed Calculates and displays: Miles driven Miles per gallon Kilometers driven Liters of fuel consumed Kilometers per liter Incorporate some Selection structure logic into it: If a vehicle gets less than 15 miles per gallon, display the message: "Your vehicle has criminally low fuel efficiency." If it gets 15...
Write a Python program that does the following: Obtains the following input from a user: Mileage at beginning of measurement period Mileage at end of measurement period Gallons of fuel consumed Calculates and displays: Miles driven Miles per gallon Kilometers driven Liters of fuel consumed Kilometers per liter Also incorporate some selection structure logic into it. If a vehicle gets less than 15 miles per gallon, display the message: "Your vehicle has criminally low fuel efficiency." If it gets...
I need trying to figure out how I can make create a code in Python with this exercise for I am having trouble doing so. Miles Per Gallon Calculator Write a GUI program that calculates a car's gas mileage. The program's window should have Entry widgets that let the user enter the number of gallons of gas the car holds, and the number of miles it can be driven on a full tank. When a Calculate MPG button is clicked,...
A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline consumed by the user's car and the number of miles per gallon the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the number of miles per gallon. Your program should use a globally defined constant for the number of liters per gallon. After doing this... Modify your...
Develop a Java application that provides program output in a logical manner and incorporates appropriate data types. Similar to zyBooks examples in Chapter 2, your program should prompt a user to enter a car brand, model, year, starting odometer reading, ending odometer reading, and gallons used. The output should include this information along with the estimated miles per gallon consumed by the vehicle in the format MPG: your calculation. Print each on separate lines with the appropriate labels (example, MPG:...
Propose: In this lab, you will complete a Python program with one partner. This lab will help you with the practice modularizing a Python program. Instructions (Ask for guidance if necessary): To speed up the process, follow these steps. Download the ###_###lastnamelab4.py onto your desktop (use your class codes for ### and your last name) Launch Pyscripter and open the .py file from within Pyscripter. The code is already included in a form without any functions. Look it over carefully....
I did a program in computer science c++. It worked fine the first and second time when I ran the program, but suddenly it gave me an error. Then, after a few minutes, it started to run again and then the error showed up again. This is due tonight but I do not know what is wrong with it. PLEASE HELP ME! Also, the instruction for the assignment, the code, and the error that occurred in the program are below....
Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one menu item called Search. Clicking on search should prompt the user using a JOptionPane input dialog to enter a car make. The GUI should then display only cars of that make. You will need to write a second menu...