《Think Python 2e》作业实现(一): 程序之道
这是什么?
这里是《Think Python 2e》作业实现 !在这里将记录《Think Python 2e》作业的练习记录、终端信息和结果分析。
- 这是《Think Python 2e》哪个版本的作业?
《Think Python:如何像计算机科学家一样思考》第二版。这里主要参考了一个中文网页版《Think Python 2e》中译本。- 可以当成《Think Python 2e》参考答案吗?
这里主要记录了我自己完成作业时所产生的成果及习题总结,基本未参考教材所提供的答案,未免有失规范,参考答案建议还是以 绿茶出版社官方代码 为准。- 不同的解释器版本结果不尽相同,这里用的哪个版本Python解释器?
这里用了Python 3.8.6版解释器,部分用安卓Pydroid 4.01_arm64中的3.8.3版Python解释器,在线解释器用教程推荐的PythonAnywhere中的3.8版Python解释器。
习题1-1:在交互模式中故意犯错
【习题 1.1.1】 在print
函数中,如果漏掉一个括号,或者两个都漏掉,会发生什么?
情况1:漏掉左括号且print
后无空格
- 练习记录:
>>> print'Hello, World!')
File "<stdin>", line 1
print'Hello, World!')
^
SyntaxError: invalid syntax
>>> print'Hello, World!'
File "<stdin>", line 1
print'Hello, World!'
^
SyntaxError: invalid syntax
- 终端信息:
序号 | 终端信息 | 含义 |
---|---|---|
1 | SyntaxError: invalid syntax | 语法错误:无效的语法 |
- 结果分析:解释器把
print'Hello
看成一个值了
情况2:漏掉左括号,print
后有空格
- 练习记录:
>>> print 'Hello, World!')
File "<stdin>", line 1
print 'Hello, World!')
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('Hello, World!'))?
>>> print 'Hello, World!'
File "<stdin>", line 1
print 'Hello, World!'
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('Hello, World!')?
- 终端信息:
序号 | 终端信息 | 含义 |
---|---|---|
1 | SyntaxError: Missing parentheses in call to ‘print’. Did you mean print(‘Hello, World!’)? | 语法错误:调用时缺少括号 |
- 结果分析:Python3 中
print
是一个函数、需要括号,所以这里特别说明缺少括号并给出了可能的建议
情况3:缺少右括号
- 练习记录:
>>> print('Hello, World!'
... )
Hello, World!
- 终端信息:
序号 | 终端信息 | 含义 |
---|---|---|
1 | … | 等待继续输入 |
- 结果分析:解释器未发现函数右括号,认为未输入完毕,等待继续输入,输入函数结尾的标志
)
后,语句执行完成、正确输出Hello, World!
【习题 1.1.2】 如果正尝试打印一个字符串,那么漏掉一个或所有引号,会发生什么?
情况1:缺少右引号
- 练习记录:
>>> print('Hello, World!)
File "<stdin>", line 1
print('Hello, World!)
^
SyntaxError: EOL while scanning string literal
- 终端信息:
序号 | 终端信息 | 含义 |
---|---|---|
1 | SyntaxError: EOL while scanning string literal | 语法错误:扫描字符串时行终止 |
- 结果分析:解释器从左往右读代码,字符串没有终止符,但是引用
print
函数的行却终止了,故“扫描字符串时行终止”
情况2:缺少左引号(含同时缺少右引号)
- 练习记录:
>>> print(Hello, World!')
File "<stdin>", line 1
print(Hello, World!')
^
SyntaxError: invalid syntax
>>> print(Hello, World!)
File "<stdin>", line 1
print(Hello, World!)
^
SyntaxError: invalid syntax
- 终端信息:
序号 | 终端信息 | 含义 |
---|---|---|
1 | SyntaxError: invalid syntax | 语法错误:无效的语法 |
- 结果分析:解释器把
World!
看成一个值了,而这样的值的命名在语法中是无效的
情况3:左右括号都缺失且把不符合值命名中无效字符都删除
- 练习记录:
>>> print(Hello,World)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Hello' is not defined
>>> print(HelloWorld)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'HelloWorld' is not defined
- 终端信息:
序号 | 终端信息 | 含义 |
---|---|---|
1 | NameError: name ‘Hello’ is not defined | 命名错误:命名没有被定义 |
- 结果分析:解释器在解析时把
Hello
或者HelloWorld
解析成一个值的命名,但这个值没定义或者初始化
【习题 1.1.3】 可以使用一个负号来表示负数,如-2。如果在数字之前放一个正号,会发生什么?如果是2++2呢?
- 练习记录:
>>> -2
-2
>>> +2++2
4
【习题 1.1.4】 在数学标记里,前置 0 是没有问题的,如 02。在 Python 中也这么做会发生什么?
- 练习记录:
>>> 02
File "<stdin>", line 1
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers
>>> 3-02
File "<stdin>", line 1
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers
>>> -022
File "<stdin>", line 1
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers
- 终端信息:
序号 | 终端信息 | 含义 |
---|---|---|
1 | SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers | 语法错误:不允许在十进制整数用前导 0 |
- 结果分析:解释器解析时不允许在十进制整数用前导 0
【习题 1.1.5】 如果在两个值之间不放任何操作符,会发生什么?
- 练习记录:
>>> 2 4
File "<stdin>", line 1
2 4
^
SyntaxError: invalid syntax
- 终端信息:
序号 | 终端信息 | 含义 |
---|---|---|
1 | SyntaxError: invalid syntax | 语法错误:无效的语法 |
- 结果分析:数字后面加空格后,解释器就认为该数字结束了,应该继续输入有效的运算符例如 *
>>> 2 * 4
8
习题1-2:用Python 解释器做计算器
【习题1.2.1】 42分42秒一共有多少秒?
- 练习记录:
>>> 42 * 60 + 42
2562
【习题1.2.2】 10公里可以换算成多少英里?提示:一英里等于1.61公里。
- 练习记录:
>>> 10 / 1.61
6.211180124223602
【习题1.2.2】 如果你花42分42秒跑完了10公里,你的平均配速(pace)是多少(每英里耗时,分别精确到分和秒)?你每小时平均跑了多少英里(英里/时)?
- 练习记录:
>>> (42 * 60 + 42) / (10 / 1.61) / 60
6.874700000000001
>>> .874700000000001 * 60
52.48200000000006
>>> (10 /1.61) / ((42 * 60 + 42) / 60 / 60)
8.727653570337614
- 结果分析:配速和每小时跑的路程都是按英里算的结果,电子书如此要求,中译本实体书是要求按1000米(公里)算,结果会有差异