Question

Write a Python program that does the following: Obtains the following input from a user: Mileage...

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 15 or more but less than 20, display:

      "Your vehicle has undesirably low fuel efficiency."

If it gets 20 or more but less than 25, display:

      "Your vehicle gets barely acceptable efficiency."

If it gets 25 or more but less than 35, display:

      "Your vehicle gets high fuel efficiency."

If it gets 35 or more, display:

      "Your vehicle gets outstanding fuel efficiency."

Also incorporate repetition:

If a user enters a non-numeric values for any of the input values, display an error message informing him of his mistake and require him to re-enter the value.

If a user enters a negative numeric values for any of the input values, display an error message informing her of her mistake and require her to re-enter the value.

Test the validity of each input separately. Do not allow a user to enter data for a variable until he enters a valid value for the previous variable.

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

Program:

flag = True
mileage1 = ""
mileage2 = ""
gallon_used = ""

while flag:
    flag1 = True
    # this loop will continue until it gets correct value for mileage
    while flag1:
        mileage1 = input("Enter mileage at beginning of measurement period: ")
        # if value is not numeric this will execute
        if not mileage1.isnumeric():
            try:
                mileage1 = float(mileage1)
                if mileage1 < 0:
                    print("Enter non negative value!!")
                    continue
                flag1 = False
            except ValueError:
                print("Enter only numeric data!!")
                continue
        else:
            mileage1 = float(mileage1)
            flag1 = False

    flag1 = True
    # this loop will continue until it gets correct value for mileage
    while flag1:
        mileage2 = input("Enter mileage at end of measurement period: ")
        if not mileage2.isnumeric():
            try:
                mileage2 = float(mileage2)
                if mileage2 < 0:
                    print("Enter non negative value!!")
                    continue
                flag1 = False
            except ValueError:
                print("Enter only numeric data!!")
                continue
        else:
            mileage2 = float(mileage2)
            if mileage2 < mileage1:
                print("Mileage at end of measurement period should be greater than at the beginning of measurement period")
                continue
            flag1 = False

    flag1 = True
    # this loop will continue until it gets correct value for gallons
    while flag1:
        gallon_used = input("Enter Gallons of fuel consumed: ")
        if not gallon_used.isnumeric():
            try:
                gallon_used = float(gallon_used)
                if gallon_used < 0:
                    print("Enter non negative value!!")
                    continue
                flag1 = False
            except ValueError:
                print("Enter only numeric data!!")
                continue
        else:
            gallon_used = float(gallon_used)
            flag1 = False

    # calculating values
    miles_driven = mileage2 - mileage1
    miles_per_gallon = miles_driven / gallon_used
    kilometers_driven = miles_driven * 1.60934
    liters_used = gallon_used * 3.78541
    kilometers_per_liters = kilometers_driven / liters_used

    print("Miles driven: ", miles_driven)
    print("Miles per gallon: ", miles_per_gallon)

    # applying conditions
    if miles_per_gallon < 15:
        print("Your vehicle has criminally low fuel efficiency.")
    elif 20 > miles_per_gallon >= 15:
        print("Your vehicle has undesirably low fuel efficiency.")
    elif 25 > miles_per_gallon >= 20:
        print("Your vehicle gets barely acceptable efficiency.")
    elif 35 > miles_per_gallon >= 25:
        print("Your vehicle gets high fuel efficiency.")
    elif miles_per_gallon >= 35:
        print("Your vehicle gets outstanding fuel efficiency.")

    # printing values
    print("Kilometers driven", kilometers_driven)
    print("Liters of fuel consumed", liters_used)
    print("Kilometers per liter", kilometers_per_liters)

    # asking the user if he/she wants to continue
    print("Do you wish to continue calculating? Enter Y for YES and N for NO: ")
    cont = input()
    if cont == 'N':
        flag = False
        print("Exiting the program")

Output:

Enter mileage at beginning of measurement period: 1ab
Enter only numeric data!!
Enter mileage at beginning of measurement period: -100
Enter non negative value!!
Enter mileage at beginning of measurement period: 100
Enter mileage at end of measurement period: 50
Mileage at end of measurement period should be greater than at the beginning of measurement period
Enter mileage at end of measurement period: 260.92
Enter Gallons of fuel consumed: 90.47
Miles driven: 160.92000000000002
Miles per gallon: 1.77871117497513
Your vehicle has criminally low fuel efficiency.
Kilometers driven 258.97499280000005
Liters of fuel consumed 342.4660427
Kilometers per liter 0.7562063402206038
Do you wish to continue calculating? Enter Y for YES and N for NO:
Y
Enter mileage at beginning of measurement period: 100.4
Enter mileage at end of measurement period: 200
Enter Gallons of fuel consumed: 80
Miles driven: 99.6
Miles per gallon: 1.2449999999999999
Your vehicle has criminally low fuel efficiency.
Kilometers driven 160.29026399999998
Liters of fuel consumed 302.8328
Kilometers per liter 0.5293028496252716
Do you wish to continue calculating? Enter Y for YES and N for NO:
N
Exiting the program

Screenshots:

Add a comment
Know the answer?
Add Answer to:
Write a Python program that does the following: Obtains the following input from a user: Mileage...
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 does the following: Obtains the following input from a user: Mileage...

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

  • The Python program will compute total mileage for all trips included : Trip: 1,2,3,4 Mileage: 200,...

    The Python program will compute total mileage for all trips included : Trip: 1,2,3,4 Mileage: 200, 400, 800, 10 Gas: 10, 17, 39, 1 As well as the fuel efficiency in miles per gallon. Your program will open the file, read it line by line and add the miles driven and the gallons gas consumed then calculate the average fuel efficiency. It will then write Once the program has read all the lines, it will write the total miles driven,...

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

  • In Visual Basics - Program 2. Mileage Write a program which allows the user to enter...

    In Visual Basics - Program 2. Mileage Write a program which allows the user to enter the distance they have traveled by car in miles and the number of gallons of gasoline they used to travel the distance. Be sure to allow for decimal places for both values. Your results should show miles per gallon and kilometers per gallon. A kilometer is 0.61 miles, and this value must be stored as a constant. Your outputs must be formatted to 2...

  • create an application in VISUAL BASIC that calculates a cars gas mileage. The formula for calculating...

    create an application in VISUAL BASIC that calculates a cars gas mileage. The formula for calculating the miles that a car can travel per gallon of gas is MPG = Miles/Gallon In the formula MPG is miles-per-gallon, miles is the number of miles that can be driven on a full tank of gas, and gallons is the number of gallons that the tank holds. The applications form should have TextBox controls that let the user enter the number of gallons...

  • Use a java program that does the following: . (10 points) Write a program as follows...

    Use a java program that does the following: . (10 points) Write a program as follows a. Prompt the user to input two positive integers: nl and n2 (nl should be less than n2) b. If the user enters the negative number(s), convert it/them to positive number(s) c. If nl is greater than n2, swap them. d. Use a while loop to output all the even numbers between nl and n2 e. Use a while loop to output the sum...

  • in Python The program will compute and display information for a company which rents vehicles to...

    in Python The program will compute and display information for a company which rents vehicles to its customers. For a specified customer, the program will compute and display the amount of money charged for that customer’s vehicle rental. 1. The program will repeatedly prompt the user to enter the following four items for a given customer (in the specified order): a. The customer's classification code (a character) b. The number of days the vehicle was rented (a number) c. The...

  • A liter is 0.264179 gallons. Write a program that will read in the number of liters...

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

  • You want to write a converter program which will let user convert distance and weight from SI to ...

    Use functions to complete this C + + program You want to write a converter program which will let user convert distance and weight from SI to the USCS system and vice versa The formulae are as follows: 1. Distance in miles- distance in kilometers x 1.60 2. Weight in kilograms weight in pounds x 2.20 The program would let user input the number that they want to convert and then choose the units to and from which they want...

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

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