半夜睡不着,代码全部帮你敲好了,大改了一下while True:
while True:
commodity = input("请输入商品货号(输入Esc退出程序):")
if commodity == 'Esc':
break
price = input("请输入商品单价:")
if not price.isdigit() or not commodity.isdigit() or float(price) <= 0 : # 判断输入是否为数字 和 是否大于零
print("输入不合法!请重新输入!")
continue
break
if commodity == 'Esc':
break
while True:
amount = input("请输入商品数量:")
if not amount.isdigit() and int(amount) < 0: # 判断输入是否为数字 和 是否大于零
print("输入不合法!请重新输入!")
else:
amount = int(amount)
total_price = float(price) * amount
temp = str(int(total_price))
break
print("总价:%.2f"%total_price)
print("现在本店有两种活动,输入1,2进行选择~")
print("1.十件8.8折,十五件7.8折")
print("2.满500减100,满800减200,满1000减300")
while True:
sale = input("请输入折扣活动:")
if sale != '2' and sale != '1':
print("输入不合法!请重新输入1 或 2 !")
continue
break
# 打折活动
if sale == '1':
if amount >= 15:
total_price = total_price * 0.78
elif amount >= 10:
total_price = total_price * 0.88
print("活动1折后价:%.2f"%total_price)
# 满减活动
elif sale == '2':
if total_price >= 1000:
total_price -= 300
elif total_price >= 800:
total_price -= 200
elif total_price >= 500:
total_price -= 100
print(total_price)
print("活动2折后价:%.2f"%total_price)
elif int(total_price) == int(temp):
print('不满足活动条件,未参与活动!')
sale = 0
while True:
try:
payment = int(input("请输入付款金额:"))
break
except ValueError:
print('输入不合法!请重新输入!')
margin = payment - total_price
if margin >= 0:
print("找零:", margin)
else:
print(f"还差{-margin}元,已记账")
goods = '|商品货号:{:^3} |商品单价:{:^3} |商品数量:{:^3} |原价:{:^5} |参与活动:{:^2} |活动后价格:{:^5} |实付款:{:^5} |找零或欠账:{:^5}\n'\
.format(commodity,price,amount,temp,sale,total_price,payment,margin)
with open('Goods.txt','a',encoding='utf-8') as f:
f.write(goods)
如果帮助到你了 给个最佳吧~ |