笨办法学 Python3 第五版(预览)(三)(1)

简介: 笨办法学 Python3 第五版(预览)(三)(1)

练习 30:假如

这是你将要输入的下一个 Python 脚本,它向你介绍了if语句。输入这个代码,确保它能够完美运行,然后我们将看看你的练习是否有所收获。

列表 30.1:ex30.py

1   people = 20
 2   cats = 30
 3   dogs = 15
 4
 5
 6   if people < cats:
 7       print("Too many cats! The world is doomed!")
 8
 9   if people > cats:
10       print("Not many cats! The world is saved!")
11
12   if people < dogs:
13       print("The world is drooled on!")
14
15   if people > dogs:
16       print("The world is dry!")
17
18
19   dogs += 5
20
21   if people >= dogs:
22       print("People are greater than or equal to dogs.")
23
24   if people <= dogs:
25       print("People are less than or equal to dogs.")
26
27
28   if people == dogs:
29       print("People are dogs.")

你应该看到的内容

1   Too many cats! The world is doomed!
2   The world is dry!
3   People are greater than or equal to dogs.
4   People are less than or equal to dogs.
5   People are dogs.
dis()

在接下来的几个练习中,我希望你运行dis()在你正在学习的一些代码上,以便更深入地了解它是如何工作的:

1   from dis import dis
2
3   dis('''
4   if people < cats:
5       print("Too many cats! The world is doomed!")
6   ''')

是你在编程时通常会做的事情。我只是希望你在这里这样做,以便为你理解正在发生的事情提供另一种可能的方式。如果dis()并没有真正帮助你更好地理解代码,那么随意这样做并忘记它。

要研究这个问题,只需将 Python 代码放在这个dis()输出旁边,然后尝试识别与字节码匹配的 Python 代码行。

练习题

在这个练习中,试着猜测if语句是什么以及它的作用是什么。在继续下一个练习之前,尝试用自己的话回答这些问题:

  1. 你认为if对下面的代码有什么影响?
  2. 为什么if下面的代码需要缩进四个空格?
  3. 如果没有缩进会发生什么?
  4. 你能否在if语句中放入来自练习 28 的其他布尔表达式?试一试。
  5. 如果你改变peoplecatsdogs的初始值会发生什么?

常见学生问题

+=是什么意思? 代码x += 1x = x + 1相同,但输入更少。你可以称之为“递增运算符”。对于-=和许多其他表达式,你以后会学到的也是一样。

练习 31:否则和如果

在上一个练习中,你解决了一些if 语句,然后试图猜测它们是什么以及它们如何工作。在学习更多之前,我将通过回答你在学习练习中提出的问题来解释一切。你做了学习练习,对吧?

  1. 你认为if对其下面的代码有什么影响?if 语句在代码中创建了所谓的“分支”。这有点像那些选择你自己冒险的书,如果你做出一个选择,就会被要求翻到一页,如果你选择另一条路,就会翻到另一页。if 语句告诉你的脚本,“如果这个布尔表达式为真,则运行其下的代码;否则跳过它。”
  2. 为什么if下面的代码需要缩进四个空格?在一行的末尾加上冒号是告诉 Python 你将创建一个新的代码“块”,然后缩进四个空格告诉 Python 哪些代码行在该块中。这与你在本书的前半部分创建函数时所做的事情完全相同。
  3. 如果没有缩进会发生什么?如果没有缩进,你很可能会产生 Python 错误。Python 希望你在以:(冒号)结尾的行之后缩进一些东西。
  4. 你能把练习 28 中的其他布尔表达式放在if 语句中吗?试试看。是的,你可以,而且它们可以尽可能复杂,尽管非常复杂的东西通常是不好的风格。
  5. 如果更改peoplecatsdogs的初始值会发生什么?因为你正在比较数字,如果更改数字,不同的if 语句将评估为True,并且其下的代码块将运行。回去放入不同的数字,看看你是否能在脑海中弄清楚哪些代码块将运行。

将我的答案与你的答案进行比较,并确保你真正理解代码“块”的概念。这对于你做下一个练习很重要,其中你将编写所有可以使用的if 语句的部分。

将这个输入并使其工作。

列表 31.1:ex31.py

1   people = 30
 2   cars = 40
 3   trucks = 15
 4
 5
 6   if cars > people:
 7       print("We should take the cars.")
 8   elif cars < people:
 9       print("We should not take the cars.")
10   else:
11       print("We can't decide.")
12
13   if trucks > cars:
14       print("That's too many trucks.")
15   elif trucks < cars:
16       print("Maybe we could take the trucks.")
17   else:
18       print("We still can't decide.")
19
20   if people > trucks:
21       print("Alright, let's just take the trucks.")
22   else:
23       print("Fine, let's stay home then.")

你应该看到什么

1   We should take the cars.
2   Maybe we could take the trucks.
3   Alright, let's just take the trucks.
dis()

我们现在到了一个dis()有点太复杂的地步。让我们只选择一个代码块来学习:

1   from dis import dis
 2
 3   dis('''
 4   if cars > people:
 5       print("We should take the cars.")
 6   elif cars < people:
 7       print("We should not take the cars.")
 8   else:
 9       print("We can't decide.")
10   ''')

我认为学习这个最好的方法是将 Python 代码放在dis()输出旁边,尝试将 Python 代码的行与其字节码匹配。如果你能做到这一点,那么你将远远领先于许多甚至不知道 Python 有dis()的 Python 程序员。

如果你搞不清楚,不要担心。这一切都是关于尽可能推动你的知识,以找到理解 Python 的新方法。

学习练习

  1. 试着猜猜elifelse在做什么。
  2. 更改carspeopletrucks的数字,然后跟踪每个if 语句,看看将打印出什么。
  3. 尝试一些更复杂的布尔表达式,比如cars > people or trucks < cars
  4. 在每行上方写出该行的英文描述。

常见学生问题

如果多个 elif 块都为 True会发生什么? Python 从顶部开始运行第一个为True的块,因此只会运行第一个。

练习 32:做决策

在这本书的前半部分,你主要只是打印出一些称为“函数”的东西,但一切基本上都是直线的。你的脚本从顶部开始运行,一直到底部结束。如果你创建了一个函数,你可以稍后运行该函数,但它仍然没有你真正需要做出决策的分支。现在你有了 ifelseelif,你可以开始编写决策性的脚本了。

在上一个脚本中,你列出了一组简单的测试,询问一些问题。在这个脚本中,你将询问用户问题,并根据他们的答案做出决定。编写这个脚本,然后多玩一下,弄清楚它的运行方式。

代码清单 32.1: ex32.py

1   print("""You enter a dark room with two doors.
 2   Do you go through door #1 or door #2?""")
 3
 4   door = input("> ")
 5
 6   if door == "1":
 7       print("There's a giant bear here eating a cheese cake.")
 8       print("What do you do?")
 9       print("1\. Take the cake.")
10       print("2\. Scream at the bear.")
11
12       bear = input("> ")
13
14       if bear == "1":
15           print("The bear eats your face off. Good job!")
16       elif bear == "2":
17           print("The bear eats your legs off. Good job!")
18       else:
19           print(f"Well, doing {bear} is probably better.")
20       print("Bear runs away.")
21
22   elif door == "2":
23       print("You stare into the endless abyss at Cthulhu's retina.")
24       print("1\. Blueberries.")
25       print("2\. Yellow jacket clothespins.")
26       print("3\. Understanding revolvers yelling melodies.")
27
28       insanity = input("> ")
29
30       if insanity == "1" or insanity == "2":
31           print("Your body survives powered by a mind of jello.")
32           print("Good job!")
33       else:
34           print("The insanity rots your eyes into a pool of muck.")
35           print("Good job!")
36
37   else:
38       print("You stumble around and fall on a knife and die. Good job!")

这里的关键点是,现在你正在将if-statements放在if-statements内部作为可以运行的代码。这是非常强大的,可以用来创建“嵌套”决策,其中一个分支导致另一个分支。

确保你理解了if-statements 中嵌套if-statements 的概念。实际上,做一些练习来真正掌握它。

你应该看到的结果

这是我玩这个小冒险游戏的情况。我表现得不太好。

1   You enter a dark room with two doors.
2   Do you go through door #1 or door #2?
3   > 1
4   There's a giant bear here eating a cheese cake.
5   What do you do?
6   1\. Take the cake.
7   2\. Scream at the bear.
8   > 2
9   The bear eats your legs off. Good job!

笨办法学 Python3 第五版(预览)(三)(2)https://developer.aliyun.com/article/1483442

相关文章
|
15天前
|
存储 安全 程序员
笨办法学 Python3 第五版(预览)(一)(4)
笨办法学 Python3 第五版(预览)(一)(4)
30 1
|
15天前
|
数据可视化 定位技术 Python
笨办法学 Python3 第五版(预览)(三)(3)
笨办法学 Python3 第五版(预览)(三)(3)
22 1
|
程序员 iOS开发 MacOS
笨办法学 Python3 第五版(预览)(一)(1)
笨办法学 Python3 第五版(预览)(一)(1)
64 1
|
15天前
|
Shell 程序员 定位技术
笨办法学 Python3 第五版(预览)(三)(4)
笨办法学 Python3 第五版(预览)(三)(4)
29 1
|
程序员 iOS开发 MacOS
笨办法学 Python3 第五版(预览)(一)(3)
笨办法学 Python3 第五版(预览)(一)(3)
67 1
|
15天前
|
Python
笨办法学 Python3 第五版(预览)(二)(1)
笨办法学 Python3 第五版(预览)(二)(1)
29 1
|
15天前
|
存储 程序员 Shell
笨办法学 Python3 第五版(预览)(二)(3)
笨办法学 Python3 第五版(预览)(二)(3)
45 0
|
15天前
|
存储 程序员 Shell
笨办法学 Python3 第五版(预览)(二)(2)
笨办法学 Python3 第五版(预览)(二)(2)
27 0
|
15天前
|
存储 Shell Ruby
笨办法学 Python3 第五版(预览)(三)(2)
笨办法学 Python3 第五版(预览)(三)(2)
23 0
|
15天前
|
存储 程序员 索引
笨办法学 Python3 第五版(预览)(二)(4)
笨办法学 Python3 第五版(预览)(二)(4)
37 1