Hi there! 我是嘟嘟~受到前辈启发,想说可以纪录一下自己练习的过程,小女子为程式超超菜鸟,此系列非教学文,仅为个人解题笔记,可能有错误或未补充详尽之处,欢迎前辈们不吝指教!也欢迎正在自学的伙伴一起讨论学习~
Day 2: Operator
给定伙食的价格(伙食的基本成本),小费百分比(作为小费添加的伙食价格的百分比)和税费百分比(作为税费添加的伙食价格的百分比),查找并打印这顿饭的总费用。
注意:请确保使用精确值进行计算,否则可能会导致舍入结果不正确!
Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result!
输入格式
有几行数字输入:
第一行有一个双精度数字(税费和小费前的餐费)。
第二行有一个整数(作为小费的百分比)。
第三行有一个整数(作为税金的百分比)。
输出格式
打印总餐费,其中是整个账单的四捨五入整数结果(含税和小费)。
样本输入
12.00208
样本输出
15
初始格式
import mathimport osimport randomimport reimport sys# Complete the solve function below.def solve(meal_cost, tip_percent, tax_percent):if __name__ == '__main__': meal_cost = float(input()) tip_percent = int(input()) tax_percent = int(input()) solve(meal_cost, tip_percent, tax_percent)
我的解答 (已修改)
def solve(meal_cost, tip_percent, tax_percent): total_cost = meal_cost * (1 + (tip_percent + tax_percent) / 100) return round(total_cost)if __name__ == '__main__': meal_cost = float(input()) tip_percent = int(input()) tax_percent = int(input()) print(solve(meal_cost, tip_percent, tax_percent))
补充:
没有看到四捨五入的条件,后来才在返回总餐费的值时加上round()
函数让餐费四捨五入到整数。四捨五入有一些要注意的地方,可以参考文章【四捨五入就用round( )?Python四捨五入的正确打开方式!】