购物车程序:
1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
7、允许查询之前的消费记录
#! /usr/bin/env python # -*- coding: utf-8 -*- # __author__ = "Deakin" # Email: [email protected] # Date: 2018/1/17 product_list=[ ["iphone",8000], ["computer",6000], ["car",30000], ["coffee",30], ["watch",500], ["book",80], ] shopping_list=[] file=open(‘user‘,‘r+‘,encoding=‘utf-8‘) f=eval(file.read()) file1=open(‘history‘,‘r+‘,encoding=‘utf-8‘) f1=eval(file1.read()) account=input("pls key in your account:") while True: if account in f: password=input("pls key in your password:") if password in f[account]: salary=int(f[account][password]) print(‘welcome %s,your balance salary is %s‘%(account,salary)) print(‘-----below is your shopping car history-----‘) if account in f1: historylist=f1[account] else: historylist=[] print(historylist) while True: select=input(‘do you want to buy sth now?yes or no:‘) if select==‘yes‘: while True: for index,item in enumerate(product_list): print(index,item) userchoice = input("pls select the product number,press ‘q‘ to quit:") if userchoice.isdigit(): userchoice=int(userchoice) if userchoice<len(product_list) and userchoice>=0: price=product_list[userchoice] if price[1]<salary: confirm=input(‘pls confirm your selection,yes or no:‘) if confirm==‘yes‘: shopping_list.append(price) salary-=price[1] print(‘add \\033[31;1m%s\\033[0m in your shopping_list,your \\033[31;1mbalance salary is %s\\033[0m‘%(price,salary)) f[account][password]=str(salary) file.seek(0) file.write(str(f)) file.tell() elif confirm==‘no‘: pass else: print(‘invalid option‘) else: print("you don‘t have enough money") elif userchoice==‘q‘: print("------shopping list------") for p in shopping_list: print(p) f1[account]=shopping_list file1.seek(0) file1.write(str(f1)) print(‘your repeated salary is %s‘%(salary)) exit() else: print(‘option invalid‘) elif select==‘no‘: exit() else: print(‘invalid option‘) else: print("password is incorrect,pls try again") else: newpwdsal={} password_new=input("account don‘t exist,pls set up your password to register:") salary_new=input("first time to log in, pls key in your salary:") sal_new=int(salary_new) newpwdsal[password_new]=sal_new #工资对应新密码 f[account]=newpwdsal #用户名对应密码和工资的小字典 file.seek(0) file.write(str(f)) file.tell()