"" 1.生成器: 生成器就是一种自定义的迭代器,本质就是迭代器 但凡函数内部包涵yield关键字,调用函数会不执行函数体代码,会得到一个返回值,该返回值就是生成器对象 2.生成器表达式: g=(i**2 for i in range(1,10) if i>7) print(next(g)) print(next(g)) 3.内置函数: 4.面向过程编程: 注册功能: # 阶段1: 接收用户输入账号与密码,完成合法性校验 def talk(): while True: username = input('请输入你的用户名: ').strip() if username.isalpha(): break else: print('用户必须为字母') while True: password1 = input('请输入你的密码: ').strip() password2 = input('请再次输入你的密码: ').strip() if password1 == password2: break else: print('两次输入的密码不一致') return username, password1 # 阶段2: 将账号密码拼成固定的格式 def register_interface(username, password1): format_str = '%s:%s\n' % (username, password1) return format_str # 阶段3: 将拼好的格式写入文件 def handle_file(format_str, filepath): with open(r'%s' % filepath, 'at', encoding='utf-8') as f: f.write(format_str) def register(): user, pwd = talk() format_str = register_interface(user, pwd) handle_file(format_str, 'user.txt') register() """