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

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

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

在这个练习中,你将学习while循环,并在进行以下三个检查时使用它们:

列表 34.1: ex34.py

1   i = 0
 2   numbers = []
 3
 4   while i < 6:
 5       print(f"At the top i is {i}")
 6       numbers.append(i)
 7
 8       i = i + 1
 9       print("Numbers now: ", numbers)
10       print(f"At the bottom i is {i}")
11
12
13   print("The numbers: ")
14
15   for num in numbers:
16       print(num)

你应该看到的结果

1   At the top i is 0
 2   Numbers now: [0]
 3   At the bottom i is 1
 4   At the top i is 1
 5   Numbers now: [0, 1]
 6   At the bottom i is 2
 7   At the top i is 2
 8   Numbers now: [0, 1, 2]
 9   At the bottom i is 3
10   At the top i is 3
11   Numbers now: [0, 1, 2, 3]
12   At the bottom i is 4
13   At the top i is 4
14   Numbers now: [0, 1, 2, 3, 4]
15   At the bottom i is 5
16   At the top i is 5
17   Numbers now: [0, 1, 2, 3, 4, 5]
18   At the bottom i is 6
19   The numbers:
20   0
21   1
22   2
23   3
24   4
25   5
dis()

在我们代码之游戏的最终“支线任务”中,你将使用dis()来分析while-loop的工作原理:

1   from dis import dis
2
3   dis('''
4   i = 0
5   while i < 6:
6       i = i + 1
7   ''')

你已经看到了大部分这些字节码,所以现在轮到你去弄清楚这个dis()输出与 Python 有什么关系了。记住你可以在文档的末尾[dis()](https://docs.python.org/3/library/dis.xhtml#python-bytecode-instructions)文档中查找所有的字节码。祝你好运!

学习练习

  1. 将这个while-loop转换为一个可以调用的函数,并用一个变量替换测试中的6i < 6)。
  2. 使用这个函数来重写脚本以尝试不同的数字。
  3. 在函数参数中添加另一个变量,你可以传入它,以便你可以更改第 8 行的+ 1,这样你就可以改变增量是多少。
  4. 再次重写脚本以使用这个函数,看看会有什么影响。
  5. 重写它以使用for-loopsrange。你还需要在中间保留增量器吗?如果不去掉它会发生什么?

如果在任何时候你这样做时出现问题(很可能会),只需按住CTRL并按下cCTRL-c),程序就会中止。

常见学生问题

for-循环和**while-循环有什么区别?for-循环只能在“集合”上进行迭代(循环)。while-循环可以进行任何类型的迭代(循环)。然而,while-循环更难正确使用,通常可以用for**-循环完成许多任务。

循环很难。我该如何理解它们? 人们不理解循环的主要原因是因为他们无法跟随代码的“跳跃”。当循环运行时,它会执行其代码块,最后跳回顶部。为了可视化这一点,在循环中到处放置print语句,打印出 Python 在循环中运行的位置以及这些点上变量的设置。在循环之前、顶部、中间和底部编写print行。研究输出并尝试理解正在进行的跳跃。

练习 35:分支和函数

你已经学会了if 语句、函数和列表。现在是时候挑战你的思维了。把这个输入进去,看看你能否弄清楚它在做什么:

列表 35.1: ex35.py

1   from sys import exit
 2
 3   def gold_room():
 4       print("This room is full of gold. How much do you take?")
 5
 6       choice = input("> ")
 7       if "0" in choice or "1" in choice:
 8           how_much = int(choice)
 9       else:
10           dead("Man, learn to type a number.")
11
12       if how_much < 50:
13           print("Nice, you're not greedy, you win!")
14           exit(0)
15       else:
16           dead("You greedy bastard!")
17
18
19   def bear_room():
20       print("There is a bear here.")
21       print("The bear has a bunch of honey.")
22       print("The fat bear is in front of another door.")
23       print("How are you going to move the bear?")
24       bear_moved = False
25
26       while True:
27           choice = input("> ")
28
29           if choice == "take honey":
30               dead("The bear looks at you then slaps your face off.")
31           elif choice == "taunt bear" and not bear_moved:
32               print("The bear has moved from the door.")
33               print("You can go through it now.")
34               bear_moved = True
35           elif choice == "taunt bear" and bear_moved:
36               dead("The bear gets pissed off and chews your leg off.")
37           elif choice == "open door" and bear_moved:
38               gold_room()
39           else:
40               print("I got no idea what that means.")
41
42
43   def cthulhu_room():
44       print("Here you see the great evil Cthulhu.")
45       print("He, it, whatever stares at you and you go insane.")
46       print("Do you flee for your life or eat your head?")
47
48       choice = input("> ")
49
50       if "flee" in choice:
51           start()
52       elif "head" in choice:
53           dead("Well that was tasty!")
54       else:
55           cthulhu_room()
56
57
58   def dead(why):
59       print(why, "Good job!")
60       exit(0)
61
62   def start():
63       print("You are in a dark room.")
64       print("There is a door to your right and left.")
65       print("Which one do you take?")
66
67       choice = input("> ")
68
69       if choice == "left":
70           bear_room()
71       elif choice == "right":
72           cthulhu_room()
73       else:
74           dead("You stumble around the room until you starve.")
75
76
77   start()

你应该看到什么

这是我玩游戏的样子:

1   You are in a dark room.
 2   There is a door to your right and left.
 3   Which one do you take?
 4   > left
 5    There is a bear here.
 6   The bear has a bunch of honey.
 7   The fat bear is in front of another door.
 8   How are you going to move the bear?
 9   > taunt bear
10   The bear has moved from the door.
11   You can go through it now.
12   > open door
13   This room is full of gold. How much do you take?
14   > 1000
15   You greedy bastard! Good job!

学习练习

  1. 绘制游戏地图以及你如何在其中流动。
  2. 修复所有错误,包括拼写错误。
  3. 为你不理解的函数写注释。
  4. 添加更多内容到游戏中。你能做些什么来简化和扩展它?
  5. gold_room 有一种奇怪的方式让你输入一个数字。这种方式存在哪些错误?你能比我写的更好吗?看看 int() 的工作原理会有提示。

常见学生问题

救命!这个程序怎么运行的!? 当你在理解一段代码时遇到困难时,只需在每一行上面写一个英文注释,解释该行的作用。保持你的评论简短并与代码相似。然后要么画出代码的工作原理,要么写一段描述它的段落。如果你这样做,你就会理解它。

为什么你写了 while True 这会造成一个无限循环。

exit(0) 的作用是什么? 在许多操作系统上,一个程序可以通过 exit(0) 中止,传入的数字将指示是否有错误。如果你使用 exit(1),那么就会有一个错误,但 exit(0) 将是一个良好的退出。它与正常的布尔逻辑相反(0==False)的原因是你可以使用不同的数字来指示不同的错误结果。你可以使用 exit(100) 来表示不同的错误结果,而不同于 exit(2)exit(1)

为什么 input() 有时写成 input('> ') input 的参数是一个字符串,它应该在获取用户输入之前打印作为提示。


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


相关文章
|
14天前
|
存储 Shell Ruby
笨办法学 Python3 第五版(预览)(三)(2)
笨办法学 Python3 第五版(预览)(三)(2)
23 0
|
14天前
|
存储 程序员 索引
笨办法学 Python3 第五版(预览)(二)(4)
笨办法学 Python3 第五版(预览)(二)(4)
37 1
|
14天前
|
存储 程序员 Shell
笨办法学 Python3 第五版(预览)(二)(3)
笨办法学 Python3 第五版(预览)(二)(3)
42 0
|
程序员 Python
笨办法学 Python3 第五版(预览)(一)(2)
笨办法学 Python3 第五版(预览)(一)(2)
39 1
|
程序员 iOS开发 MacOS
笨办法学 Python3 第五版(预览)(一)(1)
笨办法学 Python3 第五版(预览)(一)(1)
64 1
|
5月前
|
Web App开发 前端开发 JavaScript
Python Selenium 浏览器打印预览
Python Selenium 浏览器打印预览
|
Rust 网络协议 IDE
Python语言学习路线及技术汇总预览
Rust语言是一种新型的系统编程语
83 0
Python语言学习路线及技术汇总预览
|
机器学习/深度学习 SQL 前端开发
Python语言学习路线及技术汇总预览
Python语言学习路线及技术汇总预览
122 0
Python语言学习路线及技术汇总预览
|
1天前
|
网络协议 算法 网络架构
Python网络编程之udp编程、黏包以及解决方案、tcpserver
Python网络编程之udp编程、黏包以及解决方案、tcpserver