# Program Arcade GamesWith Python And Pygame

 < Previous Home Next >

# Chapter 4: 随机数，循环和猜数字游戏

Hi! I'm thinking of a random number between 1 and 100.
--- Attempt 1
Guess what number I am thinking of: 50
Too high.
--- Attempt 2
Guess what number I am thinking of: 25
Too high.
--- Attempt 3
Guess what number I am thinking of: 17
Too high.
--- Attempt 4
Guess what number I am thinking of: 9
Too low.
--- Attempt 5
Guess what number I am thinking of: 14
Too high.
--- Attempt 6
Guess what number I am thinking of: 12
Too high.
--- Attempt 7
Guess what number I am thinking of: 10
Too low.
Aw, you ran out of tries. The number was 11.

## 4.1 For循环

 for i in range(5): print("I will not chew gum in class.") Output: I will not chew gum in class. I will not chew gum in class. I will not chew gum in class. I will not chew gum in class. I will not chew gum in class.

range函数控制了循环的次数。在这个例子中，循环了5次。

 for i in range(5): print("Please,") print("Can I go to the mall?") Output: Please, Can I go to the mall? Please, Can I go to the mall? Please, Can I go to the mall? Please, Can I go to the mall? Please, Can I go to the mall?

 for i in range(10): print(i) Output: 0 1 2 3 4 5 6 7 8 9

 for i in range(1, 11): print(i) Output: 1 2 3 4 5 6 7 8 9 10

# Print the numbers 1 to 10.
for i in range(10):
print(i + 1)

### 4.1.1 用比1大的数来计数

 #两种方法去打印从2到10的偶数 for i in range(2,12,2): print(i) for i in range(5): print((i + 1) * 2) Output: 2 4 6 8 10 2 4 6 8 10

 for i in range(10, 0, -1): print(i) Output: 10 9 8 7 6 5 4 3 2 1

for i in [2,6,4,2,4,6,7,4]:
print(i)

This prints:

2
6
4
2
4
6
7
4

### 4.1.2 嵌套循环

# 这打印的结果是什么是 为什么?
for i in range(3):
print("a")
for j in range(3):
print("b")

# 这打印的结果是什么? 为什么?
for i in range(3):
print("a")
for j in range(3):
print("b")

print("Done")

### 4.1.3 累积总计

total = 0
for i in range(5):
new_number = int(input("Enter a number: " ))
total += new_number
print("The total is: ", total)

$s=\sum\limits_{n=1}^{100}n$

$s=1+2+3+4+5 \ldots 98+99+100$

#和是多少？
sum = 0
for i in range(1, 101):
sum = sum + i
print(sum)

total = 0
for i in range(5):
new_number = int(input( "Enter a number: "))
if new_number == 0:
total += 1
print("You entered a total of", total, "zeros")

# a的值是什么?
a = 0
for i in range(10):
a = a + 1
print(a)

# a的值是什么?
a = 0
for i in range(10):
a = a + 1
for j in range(10):
a = a + 1
print(a)

#a的值是什么?
a = 0
for i in range(10):
a = a + 1
for j in range(10):
a = a + 1
print(a)

## 4.2 for循环举例

# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://simpson.edu/computer-science/

# Print 'Hi' 10 times
for i in range(10):
print("Hi")

# Print 'Hello' 5 times and 'There' once
for i in range(5):
print("Hello")
print("There")

# Print 'Hello' 'There' 5 times
for i in range(5):
print("Hello")
print("There")

# Print the numbers 0 to 9
for i in range(10):
print(i)

# Two ways to print the numbers 1 to 10
for i in range(1, 11):
print(i)

for i in range(10):
print(i + 1)

# Two ways to print the even numbers 2 to 10
for i in range(2, 12, 2):
print(i)

for i in range(5):
print((i + 1) * 2)

# Count down from 10 down to 1 (not zero)
for i in range(10, 0, -1):
print(i)

# Print numbers out of a list
for i in [2, 6, 4, 2, 4, 6, 7, 4]:
print(i)

# What does this print? Why?
for i in range(3):
print("a")
for j in range(3):
print("b")

# What is the value of a?
a = 0
for i in range(10):
a = a + 1
print(a)

# What is the value of a?
a = 0
for i in range(10):
a = a + 1
for j in range(10):
a = a + 1
print(a)

# What is the value of a?
a = 0
for i in range(10):
a = a + 1
for j in range(10):
a = a + 1
print(a)

# What is the value of sum?
sum = 0
for i in range(1, 101):
sum = sum + i

## 4.3 While循环

for循环常用在程序知道循环的次数的时候； while循环常用在程序需要循环至某个特殊条件发生的时候；

for i in range(10):
print(i)

while循环完成起来是这样的:

i = 0
while i < 10:
print(i)
i = i + 1

while寻呼的第一行设置了一个“哨兵”变量用于对已经执行过的循环次数进行记录。 这在for循环中是自动完成的所以可以省去一行代码。 第二行起是真正的while循环。while循环的格式也很if语句很相思。 如果条件成立，循环中的代码就会重复执行。 第四行添加了变量的递增。在for循环中这是自动发生的，同样省去了一行代码。 所以从代码角度，for循环要比while循环更简洁且易于阅读。 另一方面while寻呼可以完成各种任务。

while range(10):
print(i)

range函数只在for循环中才能工作。 不要把它用在while循环中!

### 4.3.1 使用递增运算符

i = i + 1

i += 1

while循环中看起来是这样的:

i = 0
while i < 10:
print(i)
i += 1

i *= 2

i = i * 2

i = 1
while i <= 2 ** 32:
print(i)
i *= 2

### 4.3.2 循环直至用户想退出

quit = "n"
while quit == "n":
quit = input("Do you want to quit? ")

done = False
while not done:
quit = input("Do you want to quit? ")
if quit == "y":
done = True

attack = input("Does your elf attack the dragon? ")
if attack == "y":
done = True

value = 0
increment = 0.5
while value < 0.999:
value += increment
increment *= 0.5
print(value)

i = 10
while i == 0:
print(i)
i -= 1

i = 1
while i < 10:
print(i)

## 4.4 while循环的举例

# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://simpson.edu/computer-science/

# A while loop can be used anywhere a for loop is used:
i = 0
while i < 10:
print(i)
i = i + 1

# This is the same as:
for i in range(10):
print(i)

# It is possible to short hand the code:
# i = i + 1
# With the following:
# i += 1
# This can be done with subtraction, and multiplication as well.
i = 0
while i < 10:
print(i)
i += 1

# What would this print?
i = 1
while i <= 2**32:
print(i)
i *= 2

# A very common operation is to loop until the user performs
# a request to quit
quit = "n"
while quit == "n":
quit = input("Do you want to quit? ")

# There may be several ways for a loop to quit. Using a boolean
# to trigger the event is a way of handling that.
done = False
while not done:
quit = input("Do you want to quit? ")
if quit == "y":
done = True

attack = input("Does your elf attach the dragon? ")
if attack == "y":
done = True

value = 0
increment = 0.5
while value < 0.999:
value += increment
increment *= 0.5
print(value)

# -- Common problems with while loops --

# The programmer wants to count down from 10
# What is wrong and how to fix it?
i = 10
while i == 0:
print(i)
i -= 1

# What is wrong with this loop that tries
# to count to 10? What will happen when it is run?
i = 1
while i < 10:
print(i)

## 4.5 随机数

### 4.5.1 randrange函数

import random

my_number = random.randrange(50)

my_list = ["rock", "paper", "scissors"]
random_index = random.randrange(3)
print(my_list[random_index])

### 4.5.2 random函数

my_number = random.random()

my_number = random.random() * 5 + 10

## 4.6 复习

### 4.6.3 实验

 < Previous Home Next >