要求:git
打印省、市、縣三級菜單 可返回上一級 可隨時退出程序
要求:app
用戶名和密碼存放於文件中,格式爲:egon|egon123 啓動程序後,先登陸,登陸成功則讓用戶輸入工資,而後打印商品列表,失敗則從新登陸,超過三次則退出程序 容許用戶根據商品編號購買商品 用戶選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒 可隨時退出,退出時,打印已購買商品和餘額
menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '網易':{}, 'google':{} }, '中關村':{ '愛奇藝':{}, '汽車之家':{}, 'youku':{}, }, '上地':{ '百度':{}, }, }, '昌平':{ '沙河':{ '老男孩':{}, '北航':{}, }, '天通苑':{}, '回龍觀':{}, }, '朝陽':{}, '東城':{}, }, '上海':{ '閔行':{ "人民廣場":{ '炸雞店':{} } }, '閘北':{ '火車戰':{ '攜程':{} } }, '浦東':{}, }, '山東':{}, } #part1(初步實現):可以一層一層進入 layers = [menu, ] while True: current_layer = layers[-1] for key in current_layer: print(key) choice = input('>>: ').strip() if choice not in current_layer: continue layers.append(current_layer[choice]) #part2(改進):加上退出機制 layers=[menu,] while True: if len(layers) == 0: break current_layer=layers[-1] for key in current_layer: print(key) choice=input('>>: ').strip() if choice == 'b': layers.pop(-1) continue if choice == 'q':break if choice not in current_layer:continue layers.append(current_layer[choice])
import os product_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': #一、登錄 tag=True count=0 while tag: if count == 3: print('\033[45m嘗試次數過多,退出。。。\033[0m') break uname = 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=False break else: print('\033[47m用戶名或密碼錯誤\033[0m') count+=1 elif choice == '2': uname=input('請輸入用戶名:').strip() while True: pwd1=input('請輸入密碼:').strip() pwd2=input('再次確認密碼:').strip() if pwd2 == pwd1: break else: 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=True while 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):continue pname=product_list[choice][0] pprice=product_list[choice][1] if balance_of_db > pprice: if pname in shopping_cart: # 原來已經購買過 shopping_cart[pname]['count']+=1 else: 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=0 for 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']:continue if inp in ['Y','y','yes']: # 將餘額寫入文件 src_file=db_file dst_file=r'%s.swap' %db_file with 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('購買成功,請耐心等待發貨') shopping_cart={} current_userinfo=[] tag=False else: print('輸入非法') else: print('\033[33m非法操做\033[0m')