![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
教材正文
《Python编程:从入门到实践》正文
学技术的翻译小白
记录生活,记录python学习之路。
展开
-
《Python编程:从入门到实践》11.1.2 可通过的测试
name_function.py def get_formatted_name(first, last): """生成整洁的姓名""" full_name = f"{first} {last}" return full_name.title() names.py from name_function import get_formatted_name print("Enter 'q' at any time to quit.") while True: first =原创 2020-12-31 14:49:10 · 205 阅读 · 0 评论 -
《Python编程:从入门到实践》10.4.3 重构
remember_me.py 的重点是问候用户,因此将其所有代码都放到一个名为greet_user() 的函数中: import json def greet_user(): """问候用户,并指出其名字""" filename = 'username.json' try: with open(filename) as f: username = json.load(f) except FileNotFoundError: .原创 2020-12-30 20:38:52 · 123 阅读 · 0 评论 -
《Python编程:从入门到实践》10.2 写入文件
with open(filename, 'w') as file_object: file_object.write('I love programming.') 调用open()时,第一个实参为文件名,第二个形参为写入的模式,如: 读取模式(‘r’) 写入模式(‘w’) 附加模式(‘a’)。给文件添加内容,而不是覆盖原有的内容。 读写模式(‘r+’) 如果省略了模式实参,python将以默认的只读模式打开文件。 如果要写入的文件不存在,函数open()将自动创建它。然而,以写入模式(‘w’)原创 2020-12-27 22:24:14 · 266 阅读 · 2 评论 -
《Python编程:从入门到实践》10.3 异常
try-except代码块 如果try代码块中的代码运行正常,则跳过except代码块 如果try代码块中的代码导致了错误,python将查找与之匹配的except代码块并运行其中的代码 try: print(5/0) except ZeroDivisionError: print("You can't divide by zero!") print("Give me two numbers, and I'll divide them.") print("Enter 'q' to qu原创 2020-12-27 23:39:56 · 101 阅读 · 0 评论