Very new to python. I've created a simple program that makes use of a while loop to better learn about loops. The program:
As long as the user answers "yes" I want the program to keep performing the calculation. If they say no, the loop ends.
My problem is I cannot seem to get the loop to close.
calculate_question = input("Would you like to calculate your MPG? ")
miles_driven = float(int(input("How many miles did you drive ")))
while calculate_question == "yes" or calculate_question == "y" or calculate_question == "Yes" or calculate_question == "Y":
def mpg_calc():
import random
gallons_used = float(random.randint(1,1000))
final_mpg = str(miles_driven/gallons_used)
print("Your MPG is " + final_mpg)
mpg_calc();
calculate_question = input("Would you like to calculate your MPG? ")
miles_driven = float(int(input("How many miles did you drive ")))
else:
print("Then you'll never know!")
while True
if-else
and use break
print(f'say something with a {variable}')
)int
in float(int(input("How many miles did you drive ")))
and float
in float(random.randint(1,1000))
or
conditions, calculate_question == "yes" or calculate_question == "y"
, use in
, variable in [list of values]
def mpg_calc(miles_driven: float):
gallons_used = random.randint(1, 1000)
final_mpg = miles_driven / gallons_used
print(f"Your MPG is {final_mpg}")
while True:
calculate_question = input("Would you like to calculate your MPG? ").lower()
if calculate_question in ['yes', 'y']:
miles_driven = float(input("How many miles did you drive "))
mpg_calc(miles_driven)
else:
print("Then you'll never know!")
break