103k views
3 votes
Tip Calculator: Write a program that calculates the tip you should give your server. The program should ask the user input to input the meal price, and a keyword description for the level of service with at least three options (e.g., bad, good, excellent). Each level of service should be associated with a different tip percentage.

1 Answer

3 votes

Answer:

d = {"bad": 5, "good": 15, "excellent": 20}

meal_price = float(input("Enter meal price: "))

service = input("Choose level of service [bad, good, excellent]: ")

tip = meal_price * d[service] / 100

total = meal_price + tip

print("The tip for", service, "service is %", d[service], " which is $", tip)

print("The total price is $", total)

Step-by-step explanation:

*The code is in Python.

Create a dictionary that maps the level of services to tip percentages. Note that a dictionary is used to map key and value pairs. In this case, the level of services is mapped to percentages. "bad" is mapped to 5, "good" is mapped to 15, and "excellent" is mapped to 20

Ask the user to enter the meal_price

Ask the user to choose the level of service

Calculate the tip, multiply the meal_price by percentage of the tip / 100. Note that to get the value of the level of service in the dictionary, you need to write the dictionary name, brackets and inside the brackets you need to specify the key (in this case, the key is the service)

Calculate the total price, add the meal price and tip

Print the tip percentage, tip and total

User Andrey Gubarev
by
5.1k points