Aleea S. answered 12/28/23
MIT student tutoring Computer Science
Hi! This solution provides more checks in order to avoid errors.
def get_input(prompt, input_type):
# Keeps asking for an input until a valid input is received
while True:
user_input = input(prompt)
try:
return input_type(user_input)
except ValueError:
print(f"Please enter a valid {input_type.__name__}.")
def calculate_total_and_gratuity(subtotal, gratuity_rate):
gratuity = subtotal * (gratuity_rate / 100)
total = subtotal + gratuity
return gratuity, total
# Example of how to use the get_input function
subtotal = get_input("Enter the subtotal: ", float)
gratuity_rate = get_input("Enter the gratuity percentage: ",float)
gratuity, total = calculate_total_and_gratuity(subtotal, gratuity_rate)
print(f"The gratuity is {gratuity:.2f} and the total is {total:.2f}")