python简单购物车改进版

#功能选择1.注册2.登入3.购物车4.充值5.支付
#商品列表需要是以下形式的列表['序号:商品名称:价格']
#许先创建一个longin.txt文件

shop_list=['1:电脑:5000','2:手机:3000','3:衣服:1000','4:鞋子:500','5:零食:100'] import os def login(): '''用户名和密码的注册''' username=input('请输入用户名>>>:').strip() userpwd=input('请输入密码>>>:').strip() with open(r'login.txt','r+t',encoding='utf-8')as f: for line in f: res=line.strip('\n').split(':') if res[0]==username: print('用户名已注册过') break else: f.write('%s:%s\n'%(username,userpwd)) print('注册成功') return username def enter(): '''用户的登入认证''' username = input('请输入用户名>>>:').strip() userpwd = input('请输入密码>>>:').strip() with open(r'login.txt', 'r+', encoding='utf-8')as f: for line in f: res=line.strip('\n').split(':') if res[0]==username and res[1]==userpwd: print('登入成功') break else: print('用户名还没注册') return username def shop(): '''选择商品和数量,加入新列表中''' commodity_list=[] for commodity in shop_list: print(commodity) print('6:退出') while True: input_number = input('请选择商品号码>>>:').strip() if int(input_number) not in range(1, 7): print('选择错误') elif input_number == '6': break else: shop_count = input('请选择商品数量>>>:').strip() print() for commodity in shop_list: commodity_number,commodity_name,commodity_price=commodity.split(':') if input_number==commodity_number: commodity_list.append('%s:数量:%s'%(commodity_name,shop_count)) return commodity_list def price(commodity_list): '''计算购物商品总价''' total_price=0 for commodity in shop_list: for buy in commodity_list: commodity_number, commodity_name, commodity_price = commodity.split(':') buy_name,buy_none,buy_count=buy.split(':') if commodity_name==buy_name: total_price+=int(buy_count)*int(commodity_price) print('商品总价:%s'%total_price) return total_price def recharge(username_enter): '''给账户充值''' if username_enter==False: print('你还没有登入') else: money=input('请输入你要充值的金额>>>:').strip() with open(r'login.txt','r',encoding='utf-8')as f_1,\ open(r'.login.temp.txt','w',encoding='utf-8')as f_2: for line in f_1: res=line.strip('\n').split(':') if res[0]==username_enter: f_2.write('%s:%s\n'%(line.strip('\n'),money)) print('充值成功,你充值的金额是%s' % money) else: f_2.write('%s\n'%line.strip('\n')) os.remove('login.txt') os.rename('.login.temp.txt','login.txt') return money def pay(total_price,username_enter): '''支付功能,把充值的钱减去选购商品总价,最后显示余额''' remaining_sum=0 if username_enter==False: print('你还没有登入') else: with open(r'login.txt','r',encoding='utf-8')as f_1,\ open(r'.login.temp.txt','w',encoding='utf-8')as f_2: for line in f_1: res=line.strip('\n').split(':') if res[0]==username_enter and len(res)<3: print('你的账户还没充值') if res[0]==username_enter and len(res)==3: remaining_sum=int(res[2])-int(total_price)#remaining_sum拿到余额 f_2.write('%s:%s\n'%(line.strip('\n'),remaining_sum)) print('付款成功,还剩余额:%s' % remaining_sum) else: f_2.write('%s'%line) os.remove('login.txt') os.rename('.login.temp.txt', 'login.txt') return remaining_sum username_enter=False username_login=False total_price=False money=False while True: print(''' 功能选择: 0.退出 1.注册 2.登入 3.购物 4.充值 5.支付 ''') num=input('请输入序号>>>:').strip() if num=='0':break if num=='1': username_login=login()#拿到返回值:注册成功的用户名 elif num=='2': username_enter=enter()#拿到返回值:登入成功的用户名 elif num=='3': commodity_list=shop()#拿到返回值:选购商品列表 total_price=price(commodity_list)#拿到返回值:选购商品的总价 elif num=='4': money=recharge(username_enter)#拿到返回值:拿到充值的金额 elif num=='5': pay(total_price,username_enter) else: print('序号选择错误') 2

猜你喜欢

转载自www.cnblogs.com/angelyan/p/9693721.html