循环就是当符合条件的时候,就一直执行下去。从格式上来看有两种: 一种是while循环,一种是for循环。二者的区别就是,格式不一样。主要是看你的循环条件是什么,适合用哪个,仅此而已。

The While Loop

RATE = 5.0
INITIAL_BALANCE = 10000.0
TARGET = 2 * INITIAL_BALANCE
balance = INITIAL_BALANCE
year = 0

while balance < TARGET :# while condition:
	year = year + 1
	interest = balance * RATE / 100
	balance = balance + interest
	# statement(s), 你想执行的指令
print("The investment doubled after", year, "years.")

While Loop

Loop Output Explanation
i = 0
total = 0
while total < 10 :
i = i + 1
total = total + i
print(i, total) 1 1
2 3
3 6
4 10 When total is 10, the loop condition is false, and the loop ends.
i = 0
total = 0
while total < 10 :
i = i + 1
total = total - 1
print(i, total) 1 -1
2 -3
3 -6
4 -10
. . . Because total never reaches 10, this is an “infinite loop”.
i = 0
total = 0
while total < 0 :
i = i + 1
total = total - i
print(i, total) (No output) The statement total < 0 is false when the condition is first checked, and the loop is never executed.
i = 0
total = 0
while total >= 10 :
i = i + 1
total = total + i
print(i, total) (No output) The programmer probably thought,
“Stop when the sum is at least 10.” However, the loop condition controls when the loop is executed, not when it ends。
i = 0
total = 0
while total >= 0 :
i = i + 1
total = total + i
print(i, total) (No output, program does not terminate) Because total will always be greater
than or equal to 0, the loop runs
forever. It produces no output because the print function is outside the body of the loop, as indicated by the indentation.

Application: Processing Sentinel Values

**标记值(sentinel value)**其实应该说是一个特殊条件,但是它本质上是一个value,并且不是data里面的。常用的一些标记值:-1“q”。当触发该条件时,循环结束。

total = 0.0
count = 0
salary = 0.0

while salary >= 0.0 :
	# 循环条件是salary大于等于0,所以不可能出现负值,所以-1很适合做sentinel value。
	# 因为如果输入值是小于0的,循环条件不成立。While这一块就会被跳过去。
	salary = float(input("Enter a salary or -1 to finish: "))
	if salary >= 0.0 :
		total = total + salary
		count = count + 1
	
		if count > 0 :
			average = total / count
			print("Average salary is", average)
		else :
			print("No data was entered.")
	
**# Program Run:**
# Enter a salary or -1 to finish: 10000
# Enter a salary or -1 to finish: 10000
# Enter a salary or -1 to finish: 40000
# Enter a salary or -1 to finish: −1
# Average salary is 20000.0

e.g.    Common Loop algorithms

一些常用的循环语句

①sum and average Value

total = 0.0
count = 0

inputStr = input("Enter value: ")
while inputStr != "" :
	value = float(inputStr)
	total = total + value
	count = count + 1
	inputStr = input("Enter value: ")
	
	if count > 0 :
		average = total / count
	else :
		average = 0.0

②Counting Matches

negatives = 0
inputStr = input("Enter value: ")
while inputStr != "" :
	value = int(inputStr)
	
	if value < 0 :
		negatives = negatives + 1
		inputStr = input("Enter value: ")
		print("There were", negatives, "negative values.")

③prompting Until a Match is Found

valid = False
while not valid :
	value = int(input("Please enter a positive value < 100: "))
	
	if value > 0 and value < 100 :
		valid = True
	else :
		print("Invalid input.")

④Maximum and Minimum

smallest = int(input("Enter a value: "))
inputStr = input("Enter a value: ")

while inputStr != "" :
	value = int(inputStr)
	
	if value < smallest :
		smallest = value
		inputStr = input("Enter a value: ")

⑤Comparing Adjacent Values

value = int(input("Enter a value: "))
inputStr = input("Enter a value: ")

while inputStr != "" :
	previous = value
	value = int(inputStr)
	
	if value == previous :
		print("Duplicate input")
		inputStr = input("Enter a value: ")