#文件创立函数
def text_create(name,msg):#传入文件名name,文件内容msg
desktop_path='/pycharm我的代码/lianxi01/'#指定文件的创建路径,这里是我的项目的路径
full_path=desktop_path+name+'.txt'#这里是我们创建的文件的完整路径
file=open(full_path,'w')#打开文件,’w‘参数代表写入模式,如果没有就在该路径创建一个有该名称的文本,有则追加覆盖文本内容
file.write(msg)#写入传入的参数msg,即内容
file.close()#打开一个文件,就要关闭文件
print('Done')#只是为了告诉我们这个函数已执行完毕
#这是一个文本过滤器,将指定的文字censored_word替换成changed_word
#函数的第二个参数和第三个参数已经固定,调用该函数时可不用写入
def text_filter(word,censored_word='lame',changed_word='Awesome'):
return word.replace(censored_word,changed_word)#参数word使用内置的replace函数实现文字的替换
#将上面两个函数的功能合并,即创建一个文本可以在其中输入文字,但是如果信息中含有敏感词就会被默认过滤后写入文件
def censored_text_create(name,msg):
clean_msg=text_filter(msg)
text_create(name,clean_msg)
censored_text_create('Try','lame!lame!lame!lisa,lisa')
2.账号登录:
password_list=['###','12345']#创建一个列表,用于存储用户密码
#用于登录的函数
def account_login():
tries=3#用户有三次输入错误的机会,三次过后账号将被锁定
while tries>0:
password=input('Password:')
if password==password_list[-1]:#列表的最后一个才是有效密码,第一个是重置密码,其余都是失效密码
print('Login success!')
exit(0)#只要密码正确即可退出程序
elif password==password_list[0]:#重置密码功能
new_password=input('Enter a new password:')
password_list.append(new_password)
print('Your password has changed successfully!')
account_login()
else:
print('Wrong password or invalid input!')
tries=tries-1#计数器
print(tries,'times left')
else:
print('Your account has been suspended')
account_login()