《Python编程的术与道:Python语言入门》视频课程
《Python编程的术与道:Python语言入门》视频课程链接:https://edu.csdn.net/course/detail/27845
while循环
循环非常有用,因为它们让程序运行直到用户决定退出程序。 while循环设置了一个无限循环,直到用户做某事来结束循环。
什么是while循环?
while循环测试初始条件。 如果该条件为真,则循环开始执行。 每次循环结束时,都会重新评估条件。 只要条件保持为真,循环就会继续执行。 一旦条件变为假,循环就会停止执行。
一般语法
i = 1
while i < 6:
print(i)
i += 1
1
2
3
4
5
- 每个while循环都需要一个开始为true的初始条件。
- “while”语句包含要测试的条件。
- 只要条件保持为真,循环中的所有代码都将运行。
- 一旦循环中的某些内容改变了条件,使得测试不再通过,循环就会停止执行。
- 循环之后定义的任何代码将在此时运行。
#Example: Iterate until x becomes 0
x = 6
while x:
print(x)
x -= 1
6
5
4
3
2
1
# Example: Iterate until list is empty
L = ['red', 'green', 'blue']
while L:
print(L.pop())
blue
green
red
# Example: Iterate until string is empty
x = 'blue'
while x:
print(x)
x = x[1:]
blue
lue
ue
e
如果开始时条件为假,则根本不会执行while循环。
# Example: Exit condition is false at the start
x = 0
while x:
print(x)
x -= 1
break语句
使用break语句,即使while条件成立, 也可以停止循环:
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
1
2
3
continue语句
使用continue语句,我们可以停止当前迭代,然后继续下一个迭代:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
1
2
4
5
6
else语句
使用else语句,当条件不再成立时,我们可以运行一个代码块:
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
1
2
3
4
5
i is no longer less than 6
接收用户输入
可以使用input()
函数开始接受程序中的用户输入。 输入函数向用户显示消息,描述要查找的输入类型,然后等待用户输入值。 当用户按Enter键时,该值将传递给你的变量。
一般语法
接受输入的一般情况看起来像这样:
# Get some input from the user.
variable = input('Please enter a value: ')
# Do something with the value that was entered.
Please enter a value: u
你需要一个变量来保存用户输入的任何值,并且需要一条将显示给用户的消息。
举例
在以下示例中,有一个名称列表。 要求用户输入名称,然后将其添加到名称列表中。
# Start with a list containing several names.
names = ['guido', 'tim', 'jesse']
# Ask the user for a name.
new_name = input("Please tell me someone I should know: ")
# Add the new name to our list.
names.append(new_name)
# Show that the name has been added to the list.
print(names)
Please tell me someone I should know: bob
['guido', 'tim', 'jesse', 'bob']
使用while循环来保持程序运行
我们每天使用的大多数程序都会运行,直到告诉它们退出,而在后台这通常会使用while循环。
以下是如何让用户输入任意数量的名称的示例。
# Start with an empty list. You can 'seed' the list with
# some predefined values if you like.
names = []
# Set new_name to something other than 'quit'.
new_name = ''
# Start a loop that will run until the user enters 'quit'.
while new_name != 'quit':
# Ask the user for a name.
new_name = input("Please tell me someone I should know, or enter 'quit': ")
# Add the new name to our list.
names.append(new_name)
# Show that the name has been added to the list.
print(names)
Please tell me someone I should know, or enter 'quit': bob
Please tell me someone I should know, or enter 'quit': tom
Please tell me someone I should know, or enter 'quit': jack
Please tell me someone I should know, or enter 'quit': quit
['bob', 'tom', 'jack', 'quit']
在列表中最后命名为’quit’我们希望退出而不需要添加到列表中。 可以使用简单的if
测试来消除这个bug:
# Start with an empty list. You can 'seed' the list with
# some predefined values if you like.
names = []
# Set new_name to something other than 'quit'.
new_name = ''
# Start a loop that will run until the user enters 'quit'.
while new_name != 'quit':
# Ask the user for a name.
new_name = input("Please tell me someone I should know, or enter 'quit': ")
# Add the new name to our list.
if new_name != 'quit':
names.append(new_name)
# Show that the name has been added to the list.
print(names)
Please tell me someone I should know, or enter 'quit': tom
Please tell me someone I should know, or enter 'quit': bob
Please tell me someone I should know, or enter 'quit': jack
Please tell me someone I should know, or enter 'quit': quit
['tom', 'bob', 'jack']
意外的无限循环 (死循环)
看一下下面的例子。 你能说出为什么这个循环永远不会停止吗?
current_number = 1
# Count up to 5, printing the number each time.
while current_number <= 5:
print(current_number)
1
1
1
1
1
...
这里输出是假的,因为如果真正运行它输出将填满浏览器。 你可以尝试在计算机上运行它,但你知道如何中断失控进程:
- 在大多数系统上,Ctrl-C将中断当前正在运行的程序。
循环永远运行,因为测试条件无法失败。 程序员可能意味着添加一行,每次通过循环将current_number增加1:
current_number = 1
# Count up to 5, printing the number each time.
while current_number <= 5:
print(current_number)
current_number = current_number + 1
1
2
3
4
5
下面一个意外无限循环的又一个例子:
current_number = 1
# Count up to 5, printing the number each time.
while current_number <= 5:
print(current_number)
current_number = current_number - 1
1
0
-1
-2
-3
...
在这个例子中,我们不小心开始倒计时。current_number
的值将始终小于5,因此循环将永远运行。