Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

FOR PYTHON 3 ONLY Write code underneath the 2 buttons as follows: Perform the fo

ID: 3579786 • Letter: F

Question

FOR PYTHON 3 ONLY

Write code underneath the 2 buttons as follows:

Perform the following error checking on user input when the "Calculate mpg" button is clicked.

If user enters 0 or negative values, display error message

If user enter non-numeric values, display error message

When the "Calculate mpg" button is clicked and user input is error free, calculate the miles per gallon based upon used input. Remember to convert string input to floats.

Display answer with mpg formatted to 1 or 2 decimal place.

Calulcations must be made only if there are no errors in the input.

When the "Exit" button is clicked, exit the program.

Testing & Sample output

Before testing the script, I created test Data sets. These are sets of test data whose output value has been calculated. When the script is run across these test values, the answers produced by the script should be the same as my calculated value.

Test Data Sets

Test Data Set #1

Test Data Set #2

Gallons

3

Miles Travelled

58

Calculated mpg

19.3

Gallons

3

Miles Travelled

100

Calculated mpg

33.3

Sample output from my implementation is as follows:

Error condition

Error condition

Error!

mpg is not formatted to 2 decimal places

Correct Output

Submit Instructions

Test Data Set #1

Test Data Set #2

Gallons

3

Miles Travelled

58

Calculated mpg

19.3

Gallons

3

Miles Travelled

100

Calculated mpg

33.3

20 Maya I olappa 3. Miles Per Gallon Calculator Write a GUI program that calculates a car's gas mileage. The program's window should have Entry that let the user enter the number of gallons of gas the car holds, and the number of miles it can driven a full tank. When a Calculate MPG button is clicked, the program should display the number of miles that the car may be driven per gal- lon of gas. Use the following formula to calculate miles-per-gallon: MPG gallons miles Additional Specifications The program should display an error message when User inputs non-numeric values User enters a value that is equal to or less than 0

Explanation / Answer


import tkinter
import tkinter.messagebox

class MpgCalculatorGUI:
    def __init__(self):

        # Create the main window.
        self.main_window = tkinter.Tk()

        # Create three frames to group widgets.
        self.top_frame = tkinter.Frame(self.main_window)
        self.mid_frame = tkinter.Frame(self.main_window)
        self.bottom_frame = tkinter.Frame(self.main_window)

        # Create the widgets for the top frame.
        self.prompt_label = tkinter.Label(self.top_frame, text="Enter your gas tank capacity in gallons.")
        self.capacity_entry = tkinter.Entry(self.top_frame,width=5)

        # Pack the frame's widgets.
        self.prompt_label.pack(side="left")
        self.capacity_entry.pack(side="left")


        # Create the widgets for the mid frame.
        self.prompt_label = tkinter.Label(self.mid_frame, text="Enter how many miles you can travel on a full tank.")
        self.miles_entry = tkinter.Entry(self.mid_frame, width=5)

        self.prompt_label.pack(side="left")
        self.miles_entry.pack(side="left")

        self.calc = tkinter.Button(self.bottom_frame, text="Calculate", command=self.mpg)
        self.quit = tkinter.Button(self.bottom_frame, text="Quit", command=self.main_window.destroy)

        # Pack the buttons.
        self.calc.pack(side="left")
        self.quit.pack(side="left")

        # Pack the frames.
        self.top_frame.pack()
        self.mid_frame.pack()
        self.bottom_frame.pack()

        # Enter the tkinter main loop.
        tkinter.mainloop()

    # The convert method is a callback function for
    # the Calculate button.

    def mpg(self):
        # Get the value entered by the user into the
        # capacity_entry widget.
        try:
            capacity = float(self.capacity_entry.get())
            miles = float(self.miles_entry.get())

        except ValueError:
            self.message = "Please enter a number value for tank capacity and/or miles."
            tkinter.messagebox.showinfo("Error", self.message)
            return False

        # Calculate the MPG.
        mpg = miles/capacity

        self.message = 'Your MPG is: ' + str(format(mpg, ',.2f'))

        # Display the results in an info dialog box.
        tkinter.messagebox.showinfo("Calculation", self.message)

# Create an instance of the mpgcalculatorGUI class.
mpg_calc = MpgCalculatorGUI()