Question
How can I change the below code? Miles Per Gallon Use input to prompt for float miles driven Keep promping for miles until the entry
How can I change the below code?
Miles Per Gallon
Use input to prompt for float miles driven Keep promping for miles until the entry is greater than 0
Use input to prompt for float gallons used Keep promping for gallons until the entry is greater than 0
Calculate miles per gallon as miles didived by gallons, rounded to 2 decimal places
Display miles, gallons and miles per gallon
Example
Input: (bold in computer prompt; other iis user response)
Enter miles: -500 Enter miles: 500 Enter gallons: 0 Enter gallons: 18 Display
Miles = 500.0 Gallons = 18.0 Miles Per Gallon = 27.78 Be sure to fill out author and date DO NOTwrite a new program Fix the current program
# input | |
isErrorGallon = True | |
isErrorMile = True | |
while isErrorMile: | |
try: | |
miles = float(input("Plesse enter miles driven: ")) | |
if miles >= 0: | |
isErrorMile = False | |
except: | |
isErrorMile = True | |
while isErrorMile: | |
try: | |
gallons = float(input("Plesse enter gallons used: ")) | |
if gallons >= 0: | |
isErrorGallon = False | |
except: | |
isErrorMile = True | |
# calculation | |
MPG = miles / gallons | |
# output | |
print("Miles =", miles, " Gallons =", gallons, " Miles Per Gallon=", MPG) |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started