某零售商正以六折的价格出售多种已停产的产品。零售商希望帮助顾客确定商品的降价,方法是在货架上放一张打印好的折扣表,显示原价和打折后的价格。编写一个程序,使用循环生产这个表,显示原始价格、折扣金额和购买产品的新价格。确保折扣金额和新价格在显示时四舍五入到小数点后两位。
# 自定义几种商品价格
ran=[25.25,268.4,58.26,58.48,29.62,31.27]
#使用for循环
for x in ran:
sale=x*0.6
money=x-0.6
print("原价为%.2f:"%x)
print("现价为%.2f:"%sale)
print("优惠金额为%.2f:"%money)
使用 while循环
每次输入一个新的商品价格 当输入价格为0时结束
# 设置初始值
old_price=float(input("请输入价格"))
# 开始循环
# 设置循环条件 当用户输入零时循环结束
while old_price!=0:
new_price=0.6*old_price
discount=old_price-new_price
print("原始价格为:%d"%old_price)
print("折扣金额为:%.2f"%discount)
print("新价格为:%.2f"%new_price)
old_price = float(input("请输入价格"))