个人练习(一)
刚入门的一个小白,找本书看完了基础,自己根据条件写了一个简单的小练习:
这个接口主要实现1.在本目录下创建一个保存账户和密码的txt文件 2.支持用户自我创建账户和密码 3.能够查询到用户的数量 4.如果更改掉基于目录下创建的txt文件,可能会导致错误
算是我自学记录努力的一个过程,每天不断在学习中!有什么多余或者更好的建议往大神指点指点。。帮助下我这小白...
代码如下:
def Main(): #选择界面自动运行函数 is_choice = Prompt() #is_choice 是选择的0,1,2等等 while True: if is_choice == ‘0‘: is_choice = Prompt() elif is_choice == ‘1‘: is_choice = Create() elif is_choice == ‘2‘: Login() is_choice = Prompt() elif is_choice == ‘3‘: show=Information() print(‘该数据中存在‘+ str(len(show)) + ‘个用户:‘) for s in show: print(‘用户:‘+s) is_choice = Prompt() #显示界面def Prompt(): print(‘请选择您的操作\n请输入数字代表选择:\n0.主界面 1.创建账户 2.登陆账户 3.查询账户‘) is_num = input(‘请选择:‘) return is_num #创建账户 def Create(): is_name = input(‘请输入您要创建的账户:‘) keep = True keep = Judge(is_name) while keep: with open(‘user.txt‘,‘a‘) as fu: fu.write(is_name+‘\n‘) is_psword = input(‘请输入你创建的账户密码:‘) with open(‘psword.txt‘,‘a‘) as fp: fp.write(is_psword+‘\n‘) break is_numer = Prompt() return is_numer #判断该用户是否注册过def Judge(name): _file = ‘user.txt‘ lister = [] kip = True try: with open(_file,‘r‘) as ff: lister=ff.read().splitlines() except FileNotFoundError: with open(_file, mode=‘w‘, encoding=‘utf-8‘) as ff: pass for val in lister: if val == name: print(‘不好意思,用户已存在,请重新创建‘) kip = False break else: kip = True return kip #查询账户信息def Information(): user_name = [] try: _file = ‘user.txt‘ with open(_file,‘r‘) as ff: user_name=ff.read().splitlines() except FileNotFoundError: with open(_file, mode=‘w‘, encoding=‘utf-8‘) as ff: pass return user_name #登陆账户def Login(): name_up = input(‘请输入您的登录名:‘) kap = is_Login(name_up) if kap: psword_up = input(‘请输入您的账户密码:‘) pa_Word(name_up,psword_up) #判断登录名是否存在账户中def is_Login(name): user_up = [] is_NaN = True kap = False try: _file = ‘user.txt‘ with open(_file,‘r‘) as ff: user_up=ff.read().splitlines() except FileNotFoundError: print(‘您输入的用户不存在,请核实后输入!‘) if len(user_up): for i in user_up: if name == i: kap = True else: #print(‘该用户不存在!‘) continue else: print(‘该用户不存在!‘) is_choice = Prompt() return kap #判断用户输入的密码 def pa_Word(name,word): _user = ‘user.txt‘ _pass = ‘psword.txt‘ us_user = [] us_pass = [] with open(_user,‘r‘) as ff: us_user=ff.read().splitlines() with open(_pass,‘r‘) as ff: us_pass=ff.read().splitlines() user_index = us_user.index(name) if us_pass[user_index] != word: print(‘不好意思,你的密码输入错误,请重新选择!‘) else: print(‘恭喜您,登陆成功!亲爱的:‘+name) Main()
原文地址:https://www.cnblogs.com/ifdashui/p/10579831.html
时间: 2024-11-05 10:16:18