格式化输出
本课属于 Coddy Python 学习旅程的“基础知识”单元。第 34 课,共 77 课。
挑战
入门这个项目的最后一步是设置输出格式!
例如,对于以下输入:
100 #bill
5 #tip percent
2 #number of people按以下格式输出:
Bill Split Calculator
Total (including tip): $105.0
Each person pays: $52.5重要的格式说明:
- 输出应与所示内容完全相同,包括所有大写字母和符号
- 不要对数字进行四舍五入,使用 Python 默认的浮点数格式(不要使用 .2f 等格式)
- 数字转换为浮点数类型后,Python 会显示不同的小数位数(例如,105.0、52.5、164.28571428571428)
- 使用 f-string 直接插入计算出的值:
f"${variable_name}"
亲手试试
print("Bill Split Calculator")
bill_amount = float(input())
tip_percentage = float(input())
num_people = int(input())
tip_amount = (tip_percentage / 100) * bill_amount
total_amount = bill_amount + tip_amount
amount_per_person = total_amount / num_people
print(total_amount)
print(amount_per_person)基础知识中的所有课
自己练习: Python 在线编译器