Python代码之购物车

 
 


# -*- coding:utf-8 -*-
#Author:Mr Gan

不足之处:输入商品编号时必须是整型,其他类型会报错
import sys
balance = int(input("your money:"))
shopping_bag = [] #创建购物包
shopping_list = [["iphone",5800],["watch",2600],["book",56],["computer",7800]]

while True:
for index,i in enumerate (shopping_list):
print(index,i)
pro_num = input("production number:")

with open("余额文件",'w+') as left_money:
if pro_num == "q":
left_money.write(balance)
print("your balance left \033[31;1m%s\033[0m" % balance)
sys.exit()
elif pro_num.isdigit(): #判断输入的商品编号是否是数字
pro_num = int(pro_num)
#pro_name = shopping_list[pro_num]

if 0<= pro_num <len(shopping_list):# and pro_name[1] <= balance: # 动态判断,不要写死掉
pro_name = shopping_list[pro_num]
if pro_name[1] <= balance:
shopping_bag.append(shopping_list[pro_num][0])
balance -= pro_name[1]
else:
print("your banlance is not ennough,please choose others")
else:
print("no production,choose again")
for i in shopping_bag: #遍历已购买的商品
print("\033[31;1m%s\033[0m" % i)#打印所有已经购买的商品
print("your balance left \033[31;1m%s\033[0m" % balance)
pass

转载于:https://www.cnblogs.com/a666/p/6825247.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值