函数的定义

如果现在有一个需求需要实现用户登录注册的功能,我们该怎么实现呢?

注册

username = input('username: ').strip()
pwd = input('password: ').strip()

with open('38a.txt', 'a', encoding='utf8') as fa:
fa.write(f"{username}:{pwd}\n")
fa.flush()

登录

inp_username = input('username: ').strip()
inp_pwd = input('password: ').strip()

with open('38a.txt', 'rt', encoding='utf8') as fr:
for user_info in fr:
user_info = user_info.strip('\n')
user_info_list = user_info.split('😂
if inp_username == user_info_list[0] and inp_pwd == user_info_list[1]:
print('login successful')
break
else:
print('failed')

什么是函数:函数就是定义好的一个工具具有一定的功能。
为什么要使用函数,函数:
程序冗长,扩展性差,可读性差

posted @ 2019-08-08 15:34  大海一个人听  阅读(332)  评论(0编辑  收藏  举报