''' 購物車(註冊、登陸、購物、購物車、結賬) '''python
shopping_car_dict=dict() money=[0] def input_username_pwd(): username=input('username:') pwd=input('pwd:') return username,pwd def goods_get(): with open('goods.txt','r',encoding='utf8')as fr: fr1=fr.read() fr2=eval(fr1) return fr2 fr2=goods_get() def register(): print('歡迎來到註冊功能:') username,pwd=input_username_pwd() with open('user_info.txt','a',encoding='utf8')as fa: fa.write(f'{username}:{pwd}|') def login(): print('歡迎來到登陸功能:') username,pwd=input_username_pwd() username1=f'{username}:{pwd}' with open('user_info.txt', 'r', encoding='utf8')as fr: fr2=fr.read() username2=fr2.split('|') if username1 in username2: print('登陸成功') else: print('密碼不對') def shopping(): print('歡迎來到購物天堂:') while True: for ind,goods in enumerate(fr2): print(f'商品編號{ind}',goods) choice=input('請選擇商品編號,退出請按q:') if choice=='q': break choice=int(choice) goods=fr2[choice] print(f'購買商品{goods}成功,\n') goods_name=goods[0] if goods_name in shopping_car_dict: shopping_car_dict[goods_name]+=1 else: shopping_car_dict[goods_name]=1 money[0]+=goods[1] print(f'你購買的商品是{shopping_car_dict},總價是{money[0]}') def shopping_car(): print('歡迎來到購物車功能:') while True: print(f'你購買的商品是{shopping_car_dict},總價是{money[0]}') choice=input('請輸入你要刪除的商品編號,退出請按q:') if choice=='q': break shopping_car_dict[choice]-=1 for goods in fr2: for choice in goods: money[0]-=goods[1] def pay(): print('歡迎來到付款中心') print(f'你購買的商品是{shopping_car_dict},總價是{money[0]}') choice=input('確認付款請按Y或y,清空購物車請按N或n:') if choice=='Y'or choice=='y': print(f'你已支付{money[0]}元,購買的商品是{shopping_car_dict}') elif choice=='N'or choice=='n': shopping_car_dict.clear() money[0]=0 print('購物車已清空') else: print('輸入不合法') func_msg=''' 1:註冊 2:登陸 3:購物 4:購物車 5:消費 ''' func_choice={ '1':register, '2':login, '3':shopping, '4':shopping_car, '5':pay, } while True: print(func_msg) lc=input('請輸入你想要選擇的功能,退出請按q:') if lc=='q': break else: func_choice[lc]()