Codewars 8 kyu Tip Calculator
Task
Complete the function, which calculates how much you need to tip based on the total amount of the bill and the service.
Verbalization
- 評価と%をdictionaryで表記する
- 評価はすべて小文字になおす
- Return 小数点切り上げの値段×tip rate
- 評価できない場合は "Rating not recognised"とかえす
Code
import math
def calculate_tip(amount, rating):
rate_dictionary = {
"terrible": 0.00,
"poor": 0.05,
"good": 0.10,
"great": 0.15,
"excellent": 0.20}
if rating.lower() not in rate_dictionary:
return "Rating not recognised"
tip_percentage = rate_dictionary[rating.lower()]
tip_amount = amount * tip_percentage
return math.ceil(tip_amount)
Reference
Pythonで小数点以下を切り捨て・切り上げ: math.floor(), math.ceil()