设计一个程序,完成(英雄)商品的购买(界面就是第一天打印的界面)
展示商品信息(折扣)->输入商品价格->输入购买数量->提示付款
输入付款金额->打印购买小票(扩展)
# 定义商品信息和折扣
goods = {'英雄笔记本': 5999, '英雄鼠标': 399, '英雄键盘': 899}
discount = {'满1000减100': 1000, '满2000减300': 2000, '满5000减1000': 5000}
# 展示商品信息和折扣
print('欢迎来到英雄商城!')
print('我们的商品有:')
for name, price in goods.items():
print(name, price)
print('我们的折扣有:')
for name, amount in discount.items():
print(name, amount)
# 输入商品价格和购买数量
name = input('请输入要购买的商品名称:')
price = goods.get(name)
if price is None:
print('没有这个商品!')
else:
quantity = int(input('请输入要购买的数量:'))
# 计算总价和折扣后的价格
total_price = price * quantity
discount_price = total_price
for name, amount in discount.items():
if total_price >= amount:
discount_price -= amount
print('总价为:', total_price)
print('折扣后的价格为:', discount_price)
# 输入付款金额并判断是否足够
while True:
payment = int(input('请输入付款金额:'))
if payment < discount_price:
print('付款金额不足,请重新输入!')
else:
break
# 打印购买小票
change = payment - discount_price
print('购买成功!')
print('找零为:', change)
print('购买小票:')
print('商品名称:', name)
print('商品单价:', price)
print('购买数量:', quantity)
print('总价:', total_price)
print('折扣后的价格:', discount_price)
print('付款金额:', payment)
print('找零:', change)