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

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()它

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

#### 学习练习

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

#### 常见学生问题

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

### 练习 35：分支和函数

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:
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":
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:
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.")
47
48       choice = input("> ")
49
50       if "flee" in choice:
51           start()
54       else:
55           cthulhu_room()
56
57
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() 的工作原理会有提示。

#### 常见学生问题

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

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

|
9天前
|

26 0
|
9天前
|

40 1
|
9天前
|

49 0
|
9天前
|

41 1
|
9天前
|

67 1
|
6月前
|
Web App开发 前端开发 JavaScript
Python Selenium 浏览器打印预览
Python Selenium 浏览器打印预览
146 0
|
Rust 网络协议 IDE
Python语言学习路线及技术汇总预览
Rust语言是一种新型的系统编程语
84 0
|

Python语言学习路线及技术汇总预览
Python语言学习路线及技术汇总预览
123 0
|
2天前
|

Python编程实验六：面向对象应用
Python编程实验六：面向对象应用
19 1