# __*__ coding: utf-8 __*__ __author__ = "david.z"menu = {'北京':{'海淀':{'五道口':{'soho':{},'网易':{},'google':{} },'中关村':{'爱奇艺':{},'汽车之家':{},'youku':{}, },'上地':{'百度':{}, }, },'昌平':{'沙河':{'老男孩':{'路飞':{'Python':{ }} },'北航':{}, },'天通苑':{},'回龙观':{}, },'朝阳':{},'东城':{}, },'上海':{'闵行':{"人民广场":{'炸鸡店':{} } },'闸北':{'火车战':{'携程':{} } },'浦东':{}, },'山东':{}, } currert_layer = menu layers = [] #进入下一层的都添加一次进来while True:for i in currert_layer: print(i) choice = input("进入程序:").strip()if not choice:continue #如果没有选择也继续if choice in currert_layer: layers.append(currert_layer) #进入下一层之前append上一层进入layers列表 currert_layer = currert_layer[choice] elif choice == 'b' or choice =='B':if len(layers) != 0: currert_layer = layers.pop() #把上一层添加进layers的删除掉else: print("已经在初始画面!") elif choice == 'q' or choice =='Q': print("程序结束!") exit()
alex 的三级菜单程序堪称经典了
# __*__ coding: utf-8 __*__ __author__ = "david.z"import os def gouwuche(): products = [ ("苹果X", 6888), ("MacPro", 14800), ("小米6", 2499), ("星巴克咖啡", 31), ("Python书籍", 80), ("Nike 鞋子 ", 799), ("儿童车", 1355) ] shopping_list = [] salary = input("请输入您的工资:")while True:if salary.isdigit(): salary = int(salary)while True:for index, item in enumerate(products): print(index, item) # print() user_choice = input("请选择你要购买的商品编号:")if user_choice.isdigit(): user_choice = int(user_choice)if user_choice < len(products) and user_choice >= 0: p_item = products[user_choice]if p_item[1] <= salary: # 买得起 shopping_list.append(p_item) salary -= p_item[1] print("您所购买的商品为\033[32;1m%s\033[0m,您的余额还剩\033[31;1m%s\033[0m" % (p_item, salary))else: print("\033[41;1m您的余额仅剩%s,请尽快充值!!\033[0m" % salary)else: print("您选择的【%s】不存在!!!" % user_choice) elif user_choice == "q" or user_choice == "Q": print ('shopping list'.center(50, '-'))for p in shopping_list: print (p) print("您的余额为:", salary) exit()else: print("您的输入有误!")else: print ("您的输入有误,请重新输入!!")breakinput_users = [] count = 0while count < 3: count += 1username = input("Username:").strip() password = input("Password:").strip() input_users.append(username) #把第一次输入的用户名 加入到列表里 account_file = "db/%s"%usernameif os.path.isfile(account_file): f = open(account_file,'r') account = eval(f.read())#加载账号数据到内存并且把字符串转成列表if username == account[0] and password == account[1]:if account[2] == 0: #账号激活状态 print("欢迎来到无人超市".center(50,'-')) gouwuche()else: exit("账号 %s 被锁定。 "%username)else: print("错误的账号或密码,你还可以输入%s次。"%(3-count))else: print("您输入的用户不存在!")else:if input_users.count(input_users[0]) == 3:#代表3次错误用户名一致,可以锁定 account[2] = 1 #锁定状态 f = open("db/%s"%input_users[0],'w') f.write(str(account)) f.close() print ("账户%s已经锁定。程序即将退出。"%account[0])
查找了好多资料,尽我目前的能力,这个购物车真的只能写成这样了。希望下章open 文件应用的多多努力。就知道shopping_list怎么灵活的存入文件,又如何灵活调用出来。