第一版:
salary = input('请输入您的总额:') if salary.isdigit(): salary = int(salary) else: quit('总额仅支持输入数字') list = [] list1=['iphoneX','mac_book','iphone8','python_book'] list2 = [7800,10000,7000,90] msg = ''' ------购物车菜单如下:---------- 0.Iphone X:%d 1.Mac Book:%d 2.Iphone 8:%d 3.Pythone Book:%d 4.退出 ------------------------------ ''' % (list2[0],list2[1],list2[2],list2[3]) print(msg) n=input('>>>>>>>>>>>') if n.isdigit(): n = int(n) else: quit('仅能输入0-4') while n>-1 and n<4: if salary-list2[n] < 0: print('您当前余额不足请及时充值') break else: msg1=''' 您当前购买的%s,已加入到购物车。 当前余额为:%d ''' % (list1[n],salary-list2[n]) print(msg1) list.append(list1[n]) #把每次购买的商品添加到空数组 salary = salary-list2[n] n = input('>>>>>>>>>>>>>>') if n.isdigit(): n=int(n) else: print('仅能输入0-4') break #输出购物车商品 print('您加入购物车的商品如下:') a = len(list) b = 0 while b
product_list = [ ('Mac',9000), ('kindle',800), ('tesla',900000), ('python book',105), ('bike',2000), ] saving = input('please input your saving:') shopping_car=[] #判断输入saving是否合法 if saving.isdigit(): saving = int(saving) while True: #输出商品列表 # for i in product_list: for i,v in enumerate(product_list,1): print(i,v[0],v[1]) # print(product_list.index(i),i[0],':',i[1]) choice=input('选择购买的是那个品编号【退出:q】') #判断输入的商品序号是否合法 if choice.isdigit(): choice = int(choice) #判断输入的商品序号是否在数组中 if choice>0 and choice