python简单的购物系统
#coding = utf-8 #2016-11-19
#我的工资是存在文件中的,执行后会判断是否存过工资,如果存过无需输入,直接购物,没存过需要输入工资
#wages.txt是存工资的文件
import os product_list = [ ['iphone6', 5888], ['联想笔记本', 8000], ['iphone7', 6888], ] shop_car = [] if os.path.getsize ('wages.txt'): # 判断是否写入工资 money = open ('wages.txt', 'r', encoding='utf-8') your_money = money.readline () money.close () while True: for item,p in enumerate (product_list): print (item, p[0], p[1]) user_choice = input ('请输入要购买的商品编号,按q退出:') if user_choice.isdigit (): user_choice = int (user_choice) p_price = product_list[user_choice][1] your_money = int(your_money) if p_price < your_money: shop_car.append(product_list[user_choice]) your_money -= p_price with open ('wages.txt', 'w', encoding='utf-8') as m: m.write(str(your_money)) print('加入购物车,剩余%d'%your_money) else: print('工资不够,努力赚钱') elif user_choice == 'q': for k,v in enumerate(shop_car): print(k,v) print('剩余钱数%d'%your_money) exit() else: print('参数不正确,请重新选择') else: your_money = input('请输入你的工资') if your_money.isdigit: your_money = int (your_money) with open ('wages.txt', 'w', encoding='utf-8') as money: money.write (str (your_money)) while True: for item, p in enumerate (product_list): print (item, p[0], p[1]) user_choice = input ('请输入要购买的商品编号,按q退出:') if user_choice.isdigit (): user_choice = int (user_choice) p_price = product_list[user_choice][1] if p_price < int(your_money): shop_car.append (product_list[user_choice]) your_money -= p_price with open ('wages.txt', 'w', encoding='utf-8') as m: m.write (str (your_money)) print ('加入购物车,剩余%d' % your_money) else: print ('工资不够,努力赚钱吧小伙') elif user_choice == 'q': for k, v in enumerate (shop_car): print (k, v) print ('剩余钱数%d' % your_money) exit () else: print ('参数不正确,请重新选择') else: exit ('您的工资不合法:')