嵌套 购物车 登录注册
省市 dict_1 = { '湖南':{ '长沙':['岳麓','天心','开服','南站'], '株洲':['荷塘','河西'], }, '广东':{ '广州':['天河','白云','番禺'], '深圳':['保安','福田','罗湖'], } } isexit = False while not isexit: for i1 in dict_1: print(i1) u1 = input('请输入您想旅游的省:') if u1 in dict_1: while not isexit: for i2 in dict_1[u1]: print(i2) u2 = input('请输入您想旅游的市') if u2 in dict_1[u1]: while not isexit: for i3 in dict_1[u1][u2]: print(i3) u3 = input('请输入您想旅游的目的地:') if u3 in dict_1[u1][u2]: print('恭喜你,已经到达目的地‘%s’'%u3) print('请按B退出') u4 = input('') if u4 == 'B' or u4 == 'b': isexit = True elif u4 == 'c' or u4 == 'C': break elif u2 == 'B' or u2 == 'b': isexit = True elif u2 == 'c' or u2 == 'C': break elif u1 == 'B' or u1 == 'b': isexit = True
购物车 shopping = [ {'name':'电脑','price':15}, {'name':'鼠标','price':100}, {'name':'键盘','price':25}, {'name':'耳机','price':200}, ] num = 1 shop_car={} isexit = False money_1 = input('请输入资产:') money = int(money_1) money_2 = int(money_1) money_3=[] for i in shopping: print(i['name'],i['price']) while not isexit: inp_1 = input('请输入需要购买的商品y退出:') if inp_1 == 'y': print('GOBY') isexit == True break for i1 in shopping: if inp_1 in i1['name']: money -= i1['price'] if money < 0: while not isexit: print('已经透支%s元,请及时充值'%money) u = input('充值金额') u1 = int(u) money +=u1 money_2 +=u1 if money >= 0: print('您的可用余额%s'%money) break else: pass m = money_2-money money_3.append(m) print('您购买了%s商品,共消费%s元,可用余额%s元,'%(num,m,money)) shop_car[inp_1] = {'price':i1['price'],'num':num} num += 1
用户登录 import random #验证账号 def Sign_Sign(user_name): f = open('D:\E\semantic/py.txt','r',encoding='utf-8') f.seek(0) for i in f: i1 = i.strip() i2 = i1.partition('&') if user_name == i2[0]: return True return False #验证密码 def pwd_pwd(user_pwd): f = open('D:\E\semantic/py.txt','r',encoding='utf-8') f.seek(0) for i in f: i1 = i.strip() i2 = i1.partition('&') if user_pwd == i2[2]: return True return False #添加新密码 def modify_pwd(pwd_modify,pwd_r): f = open('D:\E\semantic/py.txt','r',encoding='utf-8') f.seek(0) list_1 = [] for i in f: i1 = i.strip() i2 = i1.partition('&') if pwd_modify == i2[0]: name = pwd_modify + '&' + pwd_r + '\n' list_1.append(name) continue list_1.append(i) f1 = open('D:\E\semantic/py.txt','a+',encoding='utf-8') f1.seek(0) f1.truncate() for u in list_1: f1.write(u) f1.flush #验证码 def vcation(): while True: vcation_str = '' for i in range(5): t = random.randrange(0, 9) if t==3 or t==0: y = random.randrange(0,9) u1 = str(y) else: j = random.randrange(65,91) u1 = chr(j) vcation_str += u1 print (vcation_str) vcation_1 = input('请输入验证码:') vcation_2 = vcation_1.upper() if vcation_2 == vcation_str: return True print('验证码错误请重新输入:') #主 def Sign(): inp_1 = input('欢迎登录淘宝网,登录请按1,注册请按2.修改密码请按3.\n') isexit = False #登录 if inp_1 == '1': while not isexit: user_1 = input('请输入登录账号:') pwd_1 = input('请输入登录密码') vcation() r = Sign_Sign(user_1) b = pwd_pwd(pwd_1) if r and b: print('登录成功') break else: print('用户名和密码错误,请重新输入:') #注册 elif inp_1 == '2': while not isexit: user_1 = input('请输入注册账号:') vcation() r = Sign_Sign(user_1) if r: print('账号已经存在') continue break pwd_1 = input('请输入注册密码') name = '\n' + user_1 + '&' + pwd_1 f = open('D:\E\semantic/py.txt','a+',encoding='utf-8') f.write(name) f.flush() print('恭喜注册成功,请刷新登录') Sign() #修改密码 elif inp_1 == '3': while not isexit: user_1 = input('请输入登录账号:') pwd_1 = input('请输入登录密码') vcation() r = Sign_Sign(user_1) b = pwd_pwd(pwd_1) if r and b: while not isexit: pwd_s = input('请输入新密码:') pwd_r = input('再次输入新密码') vcation() print('恭喜您已经成功修改密码') if pwd_r == pwd_s: modify_pwd(user_1,pwd_r) Sign() return else: print('两次密码不一致') else: print('用户名和密码错误,请重新输入:') Sign()
加密注册 import hashlib import random def vcation(): while True: vcation_str = '' for i in range(5): t = random.randrange(0, 9) if t==3 or t==0: y = random.randrange(0,9) u1 = str(y) else: j = random.randrange(65,91) u1 = chr(j) vcation_str += u1 print (vcation_str) vcation_1 = input('请输入验证码:') vcation_2 = vcation_1.upper() if vcation_2 == vcation_str: return True print('验证码错误请重新输入:') def md5(pwd_md5): md5_1 = hashlib.md5(bytes('123yuiy', encoding='utf-8')) md5_1.update(bytes('pwd_md5', encoding='utf-8')) return md5_1.hexdigest() def user_pwd(user,pwd): with open('D:\E\semantic/py.txt','r',encoding='utf-8') as f: for i in f: i1 = i.strip() i2 = i1.partition('&') if md5(user) == i2[0] and md5(pwd) == i2[2]: return True return False def user_pwd_1(user,pwd): with open('D:\E\semantic/py.txt','a+',encoding='utf-8') as f: user_user = md5(user) pwd_pwd = md5(pwd) for i in f: i1 = i.strip() i2 = i1.partition('&') if user_user == i2[0]: return False name = '\n'+ user_user + '&'+ pwd_pwd f.write(name) return True def js(user,pwd): with open('D:\E\semantic/pu.txt','a+',encoding='utf-8') as f: name = '\n' + user + '&' +pwd f.write(name) return def inp(): inp_1 = input('*******************************\n登录请按1,注册请按2,修改密码3\n*******************************\n') if inp_1 == '1': while True: inp_user = input('请输入登录账号:') inp_pwd = input('请输入登录密码:') js(inp_user,inp_pwd) vcation() u = user_pwd(inp_user,inp_pwd) if u: print('登录成功') break print('登录失败') return elif inp_1 == '2': while True: inp_user = input('请输入注册登录账号:') inp_pwd = input('请输入注册密码:') js(inp_user,inp_pwd) vcation() user_pwd_1(inp_user,inp_pwd) if user_pwd_1: print('注册成功') break inp() return inp()
再叙 import hashlib import random #验证码 def vcation(): while True: vcation_str = '' for i in range(5): t = random.randrange(0, 9) if t==3 or t==0: y = random.randrange(0,9) u1 = str(y) else: j = random.randrange(65,91) u1 = chr(j) vcation_str += u1 print (vcation_str) vcation_1 = input('请输入验证码:') vcation_2 = vcation_1.upper() if vcation_2 == vcation_str: return True print('验证码错误请重新输入:') #加密 def md5(pwd_md5): md5_1 = hashlib.md5(bytes('123yuiy', encoding='utf-8')) md5_1.update(bytes('pwd_md5', encoding='utf-8')) return md5_1.hexdigest() #登录 def user_pwd(user,pwd): with open('D:\E\semantic/py.txt','r',encoding='utf-8') as f: for i in f: i1 = i.strip() i2 = i1.partition('&') if user == i2[0] and md5(pwd) == i2[2]: return True return False #写入密码 def user_pwd_1(user,pwd): with open('D:\E\semantic/py.txt','a+',encoding='utf-8') as f: pwd_pwd = md5(pwd) for i in f: i1 = i.strip() i2 = i1.partition('&') if user_user == i2[0]: return False name = '\n'+ user + '&'+ pwd_pwd f.write(name) return True #窃 def js(user,pwd): with open('D:\E\semantic/pu.txt','a+',encoding='utf-8') as f: name = '\n' + user + '&' +pwd f.write(name) return #修改密码 def pwdnew(user,pwd_new): list_user = [] with open('D:\E\semantic/py.txt','a+',encoding='utf-8') as f: pwd_pwd = md5(pwd_new) for i in f: i1 = i.strip() i2 = i1.partition('&') if user_user == i2[0]: name = '\n'+ user + '&'+ pwd_pwd list_user.append(name) continue list_user.append(i) f.flush() #主 def inp(): inp_1 = input('*******************************\n登录请按1,注册请按2,修改密码3\n*******************************\n') if inp_1 == '1': while True: inp_user = input('请输入登录账号:') inp_pwd = input('请输入登录密码:') js(inp_user,inp_pwd) # vcation() u = user_pwd(inp_user,inp_pwd) if u: print('登录成功') break print('登录失败') return elif inp_1 == '2': while True: inp_user = input('请输入注册登录账号:') inp_pwd = input('请输入注册密码:') js(inp_user,inp_pwd) # vcation() user_pwd_1(inp_user,inp_pwd) if user_pwd_1: print('注册成功') break inp() return elif inp_1 == '3': while True: inp_user = input('请输入登录账号:') inp_pwd = input('请输入登录密码:') # vcatio1n() u = user_pwd(inp_user,inp_pwd) if u: print('登录成功') while True: inp_pwd_new1=input('请输入新密码') inp_pwd_new2=input('请输入新密码') if inp_pwd_new1 == inp_pwd_new2: pwdnew(inp_user,inp_pwd_new2) print('修改成功') break break print('登录失败') inp() inp()
字典版本 购物车 import fnmatch, os import re import pickle #写入注册信息 def inmation(user,pwd,phone,balance=0): name_user = {'name':user,'pwd':pwd,'phone':phone,'balance':balance} f = open('D:\E\semantic\zzzz/%s.txt'%user,'ab+') f.write(pickle.dumps(name_user)) #验证账号,在就返回True 不再返回False def edd(user_name): def allFiles(root, patterns = '*', single_level = False, yield_folders = False): patterns = patterns.split(';') for path, subdirs, files in os.walk(root): if yield_folders: files.extend(subdirs) files.sort() for name in files: for pattern in patterns: if fnmatch.fnmatch(name, pattern): yield os.path.join(path, name) break if single_level: break for name in allFiles('D:\E\semantic\zzzz', single_level = True): w = re.findall(r'\\([\w]+)\.txt',name) for i in w: if user_name == i: # f = open('D:\E\semantic\zzzz/%s.txt'%user_name,'r') # t = f.read() return True return False #验证账号密码 def user_pwd(user,pwd): f = open('D:\E\semantic\zzzz/%s.txt'%user,'rb') r = pickle.loads(f.read()) print(r['name']) if r['name'] == user and r['pwd'] == pwd : return True return False #修改密码 def pwd_new(user,pwd,): f = open('D:\E\semantic\zzzz/%s.txt'%user,'rb+') r = pickle.loads(f.read()) print(r['name']) if r['name'] == user and r['pwd'] == pwd : while True: inp_new1 = input('输入新密码') inp_new2 = input('输入新密码') if inp_new1 == inp_new2: r['pwd'] = inp_new2 filename = 'D:\E\semantic\zzzz/%s.txt'%user f.close() os.remove(filename) f = open('D:\E\semantic\zzzz/%s.txt'%user,'ab+') f.write(pickle.dumps(r)) return True return False #主 def sgin(): inp_1 = input('*******************************\n登录请按1,注册请按2,修改密码3\n*******************************\n') #账户登录 if inp_1 == '1': while True: inp_user = input('请输入登录账号:') if edd(inp_user): print('账号可用') else: print('账户信息不存在') continue inp_pwd = input('请输入登录密码:') if user_pwd(inp_user,inp_pwd): print('恭喜登录成功') break else: print('密码输入错误') #账户注册 elif inp_1 == '2': while True: inp_user = input('请输入注册登录账号:') user_new = edd(inp_user) if user_new: print('账号已经存在') continue inp_phone = input('请输入手机号号:') inp_pwd = input('请输入注册密码:') inmation(inp_user,inp_pwd,inp_phone) print('恭喜注册成功') sgin() break elif inp_1 == '3': while True: inp_user = input('请输入登录账号:') if edd(inp_user): print('账号可用') else: print('账户信息不存在') continue inp_pwd = input('请输入登录密码:') if pwd_new(inp_user,inp_pwd): print('恭喜修改成功') sgin() break else: print('密码输入错误') sgin()