Question

Ive done this part but i want to know how i can change the file so...

Ive done this part but i want to know how i can change the file so it accepts inputs and a file input PLEASE HELP PYTHON

Write a program that computes the fuel efficiency of a multi-leg journey. The program will first prompt for the starting odometer reading and then get information about a series of legs. For each leg, the user enters the current odometer reading and the amount of gas used (separated by a space). The user signals the end of the trip with a blank line. The program should print out the miles per gallon achieved on each leg and the total MPG for the trip.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def main():
# introduce program
print('This program computes the fuel efficiency of a multi-leg journey.\n')

# get the starting odometer reading
odoStart = int(input('What is the starting odometer reading? '))

# initialize total miles and total fuel consumed to zero
milesTtl = 0; fuelTtl = 0

# get the next odometer reading and fuel consumption, separated by a space
odoFuel = input('\nEnter the odometer reading and fuel consumption for this leg of the trip, separated by a space: ')

# loop while entered text is not empty
while odoFuel != '' and len(odoFuel.split()) == 2:
# split entered text into odometer reading and fuel consumption
strList = odoFuel.split()
odoNext = int(strList[0])
fuelNext = int(strList[1])

# print mpg for this leg (current - (start + total miles)) / fuel consumed
print('Fuel efficiency for this leg: {:.1f}'.format((odoNext - (odoStart + milesTtl)) / fuelNext))

# update total miles with current odometer reading - starting reading
milesTtl = odoNext - odoStart

# update total fuel consumed
fuelTtl += fuelNext

# get the next odometer reading and fuel consumption, separated by a space
odoFuel = input('\nEnter the odometer reading and fuel consumption for this leg of the trip, separated by a space: ')

# print mpg for entire trip (total miles / total fuel)
if fuelTtl > 0:
print('\nFuel efficiency for entire trip: {:.1f}\n'.format(milesTtl / fuelTtl))
else:
print('\nInsufficient data was entered.')

if __name__ == '__main__':
main()

save this main file in .py format and run it will prompt for input

Algorithm (pseudocode)
# introduce program
# get the starting odometer reading
# initialize total miles and total fuel consumed to zero
# get the next odometer reading and fuel consumption, separated by a space
# loop while entered text is not empty
# split entered text into odometer reading and fuel consumption
# print mpg for this leg (current - (start + total miles)) / fuel consumed
# update total miles with current odometer reading - starting reading
# update total fuel consumed
# get the next odometer reading and fuel consumption, separated by a space
# print mpg for entire trip (total miles / total fuel)

Add a comment
Know the answer?
Add Answer to:
Ive done this part but i want to know how i can change the file so...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python program that computes the fuel efficiency of a multi-leg journey. The program will...

    Write a Python program that computes the fuel efficiency of a multi-leg journey. The program will first prompt for input for the starting odometer reading and then get information about the series of legs on the journey. For each leg, the user enters the current odometer reading and the amount of fuel used in liters (separated by a blank space). The user signals the end of the trip with a blank line. The program should print out the kilometers per...

  • 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...

    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,...

  • Please solve. How do I create my own text file? Where do I save it so...

    Please solve. How do I create my own text file? Where do I save it so that it is recognized by python? Do I have to import it into my code? the prompt: Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. An example file along with the correct output is shown below: example.txt the quick brown fox jumps over the lazy dog Enter the input file name:...

  • 23.4 Project 4: Using Pandas for data analysis and practice with error handling Python Please! 23.4...

    23.4 Project 4: Using Pandas for data analysis and practice with error handling Python Please! 23.4 PROJECT 4: Using Pandas for data analysis and practice with error handling Overview In this project, you will use the Pandas module to analyze some data about some 20th century car models, country of origin, miles per gallon, model year, etc. Provided Input Files An input file with nearly 200 rows of data about automobiles. The input file has the following format (the same...

  • You previously wrote a program that calculates and displays: Miles driven Miles per gallon Kilometers driven...

    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...

  • I want to know how to do this assignment!!! Help me please~ The first part of...

    I want to know how to do this assignment!!! Help me please~ The first part of your program should do the following: • Ask the user type of test they took. o (ACT or SAT) • If they said ACT then ask them their score o If their ACT score was between 0 and 7 say "Needs Work" o If their ACT score was between 10 and 20 say "Acceptable" o If they ACT score was above 20 say "Above...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • Question2 uses structured design implemented in C. Array of records (structs) with file I/O is needed....

    Question2 uses structured design implemented in C. Array of records (structs) with file I/O is needed. The program takes two inputs at a time. The name of a person, and, the coin value as an integer in the range 5 to 95. Input coin values should always be divisible by 5 (integer division). Names are one word strings. An example input is: Jane 30 This input line indicates that 30 cents change is to be given to Jane. Output change...

  • So I can not get this to work properly and he does not want us using...

    So I can not get this to work properly and he does not want us using an array list. Below is the directions along with the code I have(Im not sure the code is in the right format)   ITSE 2321 – OBJECT-ORIENTED PROGRAMMING JAVA Program 7 – Methods: A Deeper Look Write a program that will help an elementary school student learn multiplication. Use a SecureRandom object to produce two positive one-digit integers. The program should then prompt the student...

  • C++ Programming

    PROGRAM DESCRIPTIONIn this project, you have to write a C++ program to keep track of grades of students using structures and files.You are provided a data file named student.dat. Open the file to view it. Keep a backup of this file all the time since you will be editing this file in the program and may lose the content.The file has multiple rows—each row represents a student. The data items are in order: last name, first name including any middle...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT