0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Codewars 8 kyu Tip Calculator

Posted at

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

  1. 評価と%をdictionaryで表記する
  2. 評価はすべて小文字になおす
  3. Return 小数点切り上げの値段×tip rate
  4. 評価できない場合は "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()

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?