作业二:购物车程序
#作業(yè)二:請閉眼寫出購物車程序
#需求:
用戶名和密碼存放于文件中,格式為:egon|egon123
啟動程序后,先登錄,登錄成功則讓用戶輸入工資,然后打印商品列表,失敗則重新登錄,超過三次則退出程序
允許用戶根據商品編號購買商品
用戶選擇商品后,檢測余額是否夠,夠就直接扣款,不夠就提醒
可隨時退出,退出時,打印已購買商品和余 import osproduct_list = [['Iphone7',5800],['Coffee',30],['疙瘩湯',10],['Python Book',99],['Bike',199],['ViVo X9',2499],]shopping_cart={}
current_userinfo=[]db_file=r'db.txt'while True:print('''
登陸
注冊
購物''')choice=input('>>: ').strip()if choice == '1':#1、登陸tag=Truecount=0while tag:if count == 3:print('\033[45m嘗試次數過多,退出。。。\033[0m')breakuname = input('用戶名:').strip()pwd = input('密碼:').strip()with open(db_file,'r',encoding='utf-8') as f:for line in f:line=line.strip('\n')user_info=line.split(',')uname_of_db=user_info[0]pwd_of_db=user_info[1]balance_of_db=int(user_info[2])if uname == uname_of_db and pwd == pwd_of_db:print('\033[48m登陸成功\033[0m')# 登陸成功則將用戶名和余額添加到列表current_userinfo=[uname_of_db,balance_of_db]print('用戶信息為:',current_userinfo)tag=Falsebreakelse:print('\033[47m用戶名或密碼錯誤\033[0m')count+=1elif choice == '2':uname=input('請輸入用戶名:').strip()while True:pwd1=input('請輸入密碼:').strip()pwd2=input('再次確認密碼:').strip()if pwd2 == pwd1:breakelse:print('\033[39m兩次輸入密碼不一致,請重新輸入!!!\033[0m')balance=input('請輸入充值金額:').strip()with open(db_file,'a',encoding='utf-8') as f:f.write('%s,%s,%s\n' %(uname,pwd1,balance))elif choice == '3':if len(current_userinfo) == 0:print('\033[49m請先登陸...\033[0m')else:#登陸成功后,開始購物uname_of_db=current_userinfo[0]balance_of_db=current_userinfo[1]print('尊敬的用戶[%s] 您的余額為[%s],祝您購物愉快' %(uname_of_db,balance_of_db))tag=Truewhile tag:for index,product in enumerate(product_list):print(index,product)choice=input('輸入商品編號購物,輸入q退出>>: ').strip()if choice.isdigit():choice=int(choice)if choice < 0 or choice >= len(product_list):continuepname=product_list[choice][0]pprice=product_list[choice][1]if balance_of_db > pprice:if pname in shopping_cart: # 原來已經購買過shopping_cart[pname]['count']+=1else:shopping_cart[pname]={'pprice':pprice,'count':1}balance_of_db-=pprice # 扣錢current_userinfo[1]=balance_of_db # 更新用戶余額print("Added product " + pname + " into shopping cart,\033[42;1myour current\033[0m balance " + str(balance_of_db))else:print("買不起,窮逼! 產品價格是{price},你還差{lack_price}".format(price=pprice,lack_price=(pprice - balance_of_db)))print(shopping_cart)elif choice == 'q':print("""---------------------------------已購買商品列表---------------------------------id 商品 數量 單價 總價""")total_cost=0for i,key in enumerate(shopping_cart):print('%22s%18s%18s%18s%18s' %(i,key,shopping_cart[key]['count'],shopping_cart[key]['pprice'],shopping_cart[key]['pprice'] * shopping_cart[key]['count']))total_cost+=shopping_cart[key]['pprice'] * shopping_cart[key]['count']print("""您的總花費為: %s您的余額為: %s---------------------------------end---------------------------------""" %(total_cost,balance_of_db))while tag:inp=input('確認購買(yes/no?)>>: ').strip()if inp not in ['Y','N','y','n','yes','no']:continueif inp in ['Y','y','yes']:# 將余額寫入文件
src_file=db_filedst_file=r'%s.swap' %db_filewith open(src_file,'r',encoding='utf-8') as read_f,\open(dst_file,'w',encoding='utf-8') as write_f:for line in read_f:if line.startswith(uname_of_db):l=line.strip('\n').split(',')l[-1]=str(balance_of_db)line=','.join(l)+'\n'write_f.write(line)os.remove(src_file)os.rename(dst_file,src_file)print('購買成功,請耐心等待發(fā)貨')shopping_cart={}current_userinfo=[]tag=Falseelse:print('輸入非法')else:print('\033[33m非法操作\033[0m')購物車程序面條版 View Code
?
轉載于:https://www.cnblogs.com/sunwanling/p/9121063.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結
- 上一篇: content type 介绍
- 下一篇: MQ信息