A30459.【PY】年终大促销
2024-11-08 15:25:32
发布于:浙江
0阅读
0回复
0点赞
total_price = 0 # 总价
discount_threshold = 150 # 折扣阈值
discount_rate = 0.85 # 折扣率
# 循环读取输入
while True:
price = int(input())
if price == 0:
break
total_price += price
# 计算折扣后的价格
if total_price > discount_threshold:
discounted_price = (total_price - discount_threshold) * discount_rate + discount_threshold
else:
discounted_price = total_price
# 输出结果,保留两位小数
print(f"{discounted_price:.2f}")
初始化变量
total_price = 0 # 总价
discount_threshold = 150 # 折扣阈值
discount_rate = 0.85 # 折扣率
total_price:用于累加所有输入的价格。
discount_threshold:设定折扣阈值为150元。
discount_rate:设定折扣率为85%(即0.85)。
循环读取输入
while True:
price = int(input())
if price == 0:
break
total_price += price
while True:无限循环读取输入。
price = int(input()):读取一行输入并将其转换为整数。
if price == 0:如果输入为0,则跳出循环。
total_price += price:将读取的价格累加到总价中。
计算折扣后的价格
if total_price > discount_threshold:
discounted_price = (total_price - discount_threshold) * discount_rate + discount_threshold
else:
discounted_price = total_price
if total_price > discount_threshold:如果总价超过150元。
(total_price - discount_threshold) * discount_rate:计算超过150元部分的折扣价。
- discount_threshold:加上未打折的150元部分。
else:如果总价未超过150元,则不打折。
输出结果,保留两位小数
print(f"{discounted_price:.2f}")
f"{discounted_price:.2f}":格式化输出保留两位小数的浮点数。
这里空空如也
有帮助,赞一个