标签:代码 file isp 创建用户 soho useradd input one info
三级菜单要求:
1. 运行程序输出第一级菜单
2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单
3. 返回上一级菜单和顶部菜单
4. 菜单数据保存在文件中
代码如下:
1 import pickle 2 import sys 3 import os 4 5 menu = { 6 ‘北京‘:{ 7 ‘海淀‘:{ 8 ‘五道口‘:{ 9 ‘soho‘:{}, 10 ‘网易‘:{}, 11 ‘google‘:{} 12 }, 13 ‘中关村‘:{ 14 ‘爱奇艺‘:{}, 15 ‘汽车之家‘:{}, 16 ‘youku‘:{}, 17 }, 18 ‘上地‘:{ 19 ‘百度‘:{}, 20 }, 21 }, 22 ‘昌平‘:{ 23 ‘沙河‘:{ 24 ‘老男孩‘:{}, 25 ‘北航‘:{}, 26 }, 27 ‘天通苑‘:{}, 28 ‘回龙观‘:{}, 29 }, 30 ‘朝阳‘:{}, 31 ‘东城‘:{}, 32 }, 33 ‘上海‘:{ 34 ‘闵行‘:{ 35 "人民广场":{ 36 ‘炸鸡店‘:{} 37 } 38 }, 39 ‘闸北‘:{ 40 ‘火车战‘:{ 41 ‘携程‘:{} 42 } 43 }, 44 ‘浦东‘:{}, 45 }, 46 ‘山东‘:{}, 47 } 48 49 # 把menu菜单序列化到文件中 50 if not os.path.isfile(‘./level_3_menu.txt‘): 51 pickle.dump(menu,open(‘./level_3_menu.txt‘,‘wb‘)) 52 53 # 反序列化 54 data = pickle.load(open(‘./level_3_menu.txt‘,‘rb‘)) 55 56 current_layer = data #当前层 57 last_layers = [current_layer] #上一层 58 59 60 while True: 61 for key in current_layer: 62 print(key) 63 choice = input(">>:").strip() 64 if len(choice)==0:continue 65 if choice in current_layer: #进入下一层 66 last_layers.append(current_layer) #当前层添加到列表 67 current_layer = current_layer[choice] 68 69 if choice == "b": 70 if last_layers: 71 current_layer = last_layers.pop() 72 if choice == ‘q‘: 73 break
要求:
模拟登陆
1. 用户输入帐号密码进行登陆
2. 用户信息保存在文件内
3. 用户密码输入错误三次后锁定用户
代码如下:
import getpass import os import sys import pickle #把用户列表序列化到文件中去 if not os.path.isfile(‘./user_info.txt‘): user_info = {‘test‘:‘123456‘,‘abc‘:‘abc‘,‘liubei‘:‘123‘,‘guanyu‘:‘123‘} pickle.dump(user_info,open(‘./user_info.txt‘,‘wb‘)) #反序列化 user_info = pickle.load(open(‘./user_info.txt‘,‘rb‘)) #定义密码尝试次数 num = 3 if not os.path.isfile(‘./lock.txt‘): l = [] pickle.dump(l,open(‘./lock.txt‘,‘wb‘)) lock = pickle.load(open(‘./lock.txt‘,‘rb‘)) flag = False while flag == False: username=input("please input your username: ").strip() #用户名不能为空 if username == "": print ("\033[31;1musername or password isn‘t be empty !\033[0m") continue #判断用户是否存在 elif username not in user_info.keys(): print ("\033[31;1mThere is no such user !\033[0m") continue #判断用户有没有被锁定 elif username in lock: print ("\033[31;1muser %s is locked,now exit script !\033[0m"%username) sys.exit() while flag == False: password=getpass.getpass("password:").strip() if password == "": print ("\033[31;1musername or password isn‘t be empty !\033[0m") continue elif password == user_info[username]: print ("\033[35;5mWelcome %s !\033[0m"%username) flag = True else: num -= 1 print ("username or password is wrong,there are %d chances"% num) if num == 0: print ("\033[31;1mThe user %s is locked !\033[0m"%username) lock.append(username) pickle.dump(lock,open(‘./lock.txt‘,‘rb+‘)) sys.exit()
购物车要求:
1. 商品信息- 数量、单价、名称
2. 用户信息- 帐号、密码、余额
3. 用户可充值
4. 购物历史信息
5. 允许用户多次购买,每次可购买多件
6. 余额不足时进行提醒
7. 用户退出时 ,输出档次购物信息
8. 用户下次登陆时可查看购物历史
9. 商品列表分级
代码如下:
1 import getpass 2 import pickle 3 import sys 4 import os 5 6 #user_info = {‘user1‘: [‘123456‘, 50000], 7 # ‘user2‘: [‘123456‘, 60000] 8 # } 9 10 product_list = [[1, ‘Iphone7‘, 7000], 11 [2, ‘computer‘, 5000], 12 [3, ‘Python Book‘, 100], 13 [4, ‘Bike‘, 299], 14 [5, ‘Coffee‘, 30] 15 ] 16 17 if not os.path.isfile(‘./users_info.txt‘): 18 u = {} 19 pickle.dump(u, open(‘./users_info.txt‘, ‘wb‘)) 20 user_info = pickle.load(open(‘./users_info.txt‘,‘rb‘)) 21 22 flag = False 23 while flag == False: 24 print ("欢迎使用XXX购物商城".center(50,"*")) 25 print () 26 print ("1.注册 2.登陆 q.退出".center(50,">")) 27 28 choice = input("请选择: ") 29 if choice == "1": 30 flag_regist = False 31 while flag_regist == False: 32 useradd = input("Please input create username: ").strip() 33 if len(useradd) == 0: 34 print ("用户名不能为空") 35 elif useradd in user_info: 36 print ("该用户名已经存在,请用其他用户名注册") 37 elif not useradd.isidentifier(): 38 print ("用户名不要以数字开头") 39 else: 40 while flag_regist == False: 41 user_info[useradd]=[0,0] 42 passwd = getpass.getpass("请输入要注册用户的密码(密码至少需要6位):") 43 length = len(passwd) 44 if length == 0: 45 print ("密码不能为空") 46 elif length >=6: 47 user_info[useradd][0]=passwd 48 pickle.dump(user_info, open(‘./users_info.txt‘, ‘rb+‘)) 49 print ("成功创建用户:%s"%useradd) 50 flag_regist = True 51 52 53 elif choice == "2": 54 times = 3 55 #flag = False 56 if not os.path.isfile(‘./lockfile.txt‘): 57 lock = [] 58 pickle.dump(lock,open(‘./lockfile.txt‘,‘wb‘)) 59 lock = pickle.load(open(‘./lockfile.txt‘,‘rb‘)) 60 61 while flag == False: 62 username = input("please input your username: ").strip() 63 #password = getpass.getpass("password:").strip() 64 if username == "": 65 print("\033[31;1musername or password isn‘t be empty !\033[0m") 66 continue 67 68 if username not in user_info.keys(): 69 print("\033[31;1mThere is no such user !\033[0m") 70 continue 71 #判断用户有没有被锁定 72 if username in lock: 73 print ("\033[31;1muser %s is locked,now exit script !\033[0m"%username) 74 sys.exit() 75 76 while flag == False: 77 password = getpass.getpass("password: ").strip() 78 if password == "": 79 print ("\033[31;1musername or password isn‘t be empty !\033[0m") 80 continue 81 if password == user_info[username][0]: 82 salary = user_info[username][1] 83 if not os.path.isfile(‘./%s.txt‘ % username): 84 d = {} 85 pickle.dump(d, open(‘./%s.txt‘ % username, ‘wb‘)) 86 shopping_cart_history = pickle.load(open(‘./%s.txt‘ % username, ‘rb‘)) 87 print("登录成功,你的账号名称为:%s,当前账户可用余额为:%s元" % (username, salary)) 88 if shopping_cart_history: 89 print("你所购商品的历史信息如下:".center(50, ‘=‘)) 90 print("id\t商品名 单价 数量 总价") 91 for key in shopping_cart_history: 92 print("%s\t%s %s %s %s" % ( 93 shopping_cart_history[key][0], 94 key, 95 shopping_cart_history[key][1], 96 shopping_cart_history[key][2], 97 shopping_cart_history[key][3], 98 )) 99 print("END".center(62, ‘=‘)) 100 choice = input("是否需要充值y/n: ").strip() 101 if choice.casefold() == ‘y‘: 102 money = int(input("输入要充的金额: ")) 103 salary = user_info[username][1] + money 104 print("充值成功,当前余额为:%s" % salary) 105 print("\033[35;5mWelcome %s Into the shopping cart\033[0m\n%s" % (username, "-" * 50)) 106 flag = True 107 else: 108 print("\033[35;5mWelcome %s Into the shopping cart\033[0m".center(70,"*") % (username)) 109 flag = True 110 else: 111 times -= 1 112 print("username or password is wrong,there are %d chances" % (times)) 113 if times == 0: 114 print("\033[31;1mThe user is Forbidden login!\033[0m") 115 lock.append(username) 116 pickle.dump(lock,open(‘./lockfile.txt‘,‘rb+‘)) 117 sys.exit() 118 119 shopping_cart = {} 120 while True: 121 for product in product_list: 122 print(product) 123 p_id = input("[购买商品请输入商品id号,如果要退出系统就输入q]# ").strip() 124 if p_id.isdigit(): 125 p_id = int(p_id) 126 if p_id >= 0 and p_id <= len(product_list): 127 p_num = input("[请输要购买的该商品的数量]# ").strip() 128 if p_num.isdigit(): 129 p_num = int(p_num) 130 product_id = product_list[p_id - 1][0] 131 product_price = product_list[p_id - 1][2] 132 product = product_list[p_id - 1][1] 133 # print (product_list[p_id-1][2]*p_num) 134 pay = product_list[p_id - 1][2] * p_num 135 if salary >= pay: 136 # 判断购物车中是否有同名的商品 137 if product in shopping_cart: 138 shopping_cart[product][2] += p_num 139 shopping_cart[product][3] += pay 140 else: 141 shopping_cart[product] = [product_id, product_price, p_num, pay] 142 salary -= pay 143 print( 144 "Added product \033[31;1m%s\033[0m into shopping cart \033[31;1myour current Balance is %s\033[0m" % ( 145 product, salary)) 146 else: 147 print("余额不足,需要付%s元,你还差%s元,无法购买!" % (pay, pay - salary)) 148 choice = input("是否需要充值y/n: ").strip() 149 if choice.casefold() == ‘y‘: 150 money = int(input("输入要充的金额: ")) 151 salary = user_info[username][1] + money 152 print("充值成功,当前余额为:%s" % salary) 153 else: 154 pass 155 156 else: 157 print("没有该商品") 158 elif p_id == ‘q‘: 159 # 把本次购物清单加到历史购物清单中去 160 if shopping_cart: 161 for pro in shopping_cart: 162 # 判断历史购物清单中是否有和本次购买商品同名的 163 if pro in shopping_cart_history: 164 shopping_cart_history[pro][2] += shopping_cart[pro][2] 165 shopping_cart_history[pro][3] = shopping_cart_history[pro][1] * shopping_cart[pro][2] 166 else: 167 shopping_cart_history[pro] = [shopping_cart[pro][0], shopping_cart[pro][1], shopping_cart[pro][2], 168 shopping_cart[pro][3]] 169 # shopping_cart_history.update(shopping_cart) 170 pickle.dump(shopping_cart_history, open(‘./%s.txt‘ % username, ‘rb+‘)) 171 total_cost = 0 172 print("你本次已购以下商品".center(50, ‘*‘)) 173 print("id\t商品名 单价 数量 总价") 174 for key in shopping_cart: 175 print("%s\t%s %s %s %s" % ( 176 shopping_cart[key][0], 177 key, 178 shopping_cart[key][1], 179 shopping_cart[key][2], 180 shopping_cart[key][3], 181 )) 182 total_cost += shopping_cart[key][3] 183 print("您的总花费为:", total_cost) 184 print("您的余额为:", salary) 185 print("END".center(60, "*")) 186 break 187 else: 188 print("END".center(60, "*")) 189 break 190 user_info[username][1] = salary 191 pickle.dump(user_info, open(‘./users_info.txt‘, ‘rb+‘)) 192 elif choice == "q": 193 print ("已退出XXX购物商城系统!") 194 break
标签:代码 file isp 创建用户 soho useradd input one info
原文地址:http://www.cnblogs.com/xingjiancheng/p/7755520.html