《Python 编程从入门到实践》———— while循环

  for 循环用于针对集合中的每个元素都一个代码块,而while循环不断地运行,直到指定的条件不满足为止。Python 中 while 语句的一般形式:
  while 判断条件(condition):
    执行语句(statements)
  同样需要注意冒号和缩进。另外,在 Python 中没有 do…while 循环

# 输入
counter_number = 100
sum_value = 0
i = 0
while i <= counter_number:
    sum_value = sum_value + i
    i = i +1
print('The sum is: {0:10}'.format(sum_value))

# 输出
The sum is:       5050

循环退出:用户选择来决定是否退出
    下方的例子还引入了标志位 (flag)来对条件判断进行判断。标志很有用:在其中的任何一个事件导致活动标志变成 False 时,循环将退出。

# 输入
counter_number = 100
sum_value = 0
i = 0
flag = True
quit_message = "The value is reached the max, whether select quit. Please input Y/N to select:"
input_value = ""
while ((i <= counter_number)and(flag)):
    sum_value = sum_value + i
    i = i +1
    if (i == 50):
        input_value = input(quit_message)
        if input_value.upper() == "Y":
            flag = False
    
print('The sum is: {}'.format(sum_value))

# 输出
The value is reached the max, whether select quit. Please input Y/N to select:Y
The sum is: 1225

循环退出:break 退出循环
    要立即退出while循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用break语句。break语句用于控制程序流程,可使用它来控制哪些代码行将执行,哪些代码行不执行,从而让程序按你的要求执行你要执行的代码。在任何Python循环中都可使用break语句

# 输入
counter_number = 100
sum_value = 0
i = 0
flag = True
quit_message = "The value is reached the max, whether select quit. Please input Y/N to select:"
input_value = ""
while ((i <= counter_number)and(flag)):
    sum_value = sum_value + i
    i = i +1
    if (i == 50):
        break
    
print('The sum is: {}'.format(sum_value))

# 输出
The sum is: 1225

循环中断:continue 中断循环
    要返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句,它不像break语句那样不再执行余下的代码并退出整个循环。下方的例子是统计 1 ~ 100 之间的所有偶数的和。

# 输入
counter_number = 100
sum_value = 0
i = 0
flag = True
quit_message = "The value is reached the max, whether select quit. Please input Y/N to select:"
input_value = ""
while ((i <= counter_number)and(flag)):
    i = i +1
    if (i%2 == 0):
        sum_value = sum_value + i
        continue 
print('The sum is: {}'.format(sum_value))

# 输出
The sum is: 2550

 要避免编写无限循环,务必对每个while循环进行测试,确保它按预期那样结束。如果你希望程序在用户输入特定值时结束,可运行程序并输入这样的值;如果在这种情况下程序没有结束,请检查程序处理这个值的方式,确认程序至少有一个这样的地方能让循环条件为 False 或让 break 语句得以执行。有些编辑器(如Sublime Text)内嵌了输出窗口,这可能导致难以结束无限循环,因此不得不关闭编辑器来结束无限循环。






使用 while 循环处理列表和字典
 for 循环是一种遍历列表的有效方式,但在 for 循环中不应修改列表,否则将导致 Python 难以跟踪其中的元素。要在遍历列表的同时对其进行修改,可使用 while 循环。通过将 while 循环同列表和字典结合起来使用,可收集、存储并组织大量输入,供以后查看和显示。

# 添加元素至列表中
# 输入
users = []
flag = True
input_value = ''
while (flag):
    input_value = input("Input the user name: ")
    users.append(input_value)
    print(users[:])
    if 'Youth' in users:
        flag = False

# 输出
Input the user name: Tom
['Tom']
Input the user name: Jerry
['Tom', 'Jerry']
Input the user name: Hank
['Tom', 'Jerry', 'Hank']
Input the user name: Youth
['Tom', 'Jerry', 'Hank', 'Youth']
# 删除元素
# 输入
while 'Tom' in users:
    users.remove('Tom')
print(users)

# 输出
['Jerry', 'Hank', 'Youth']

 可使用while循环提示用户输入任意数量的信息。我们可以将用户输入的信息存储到字典中。

# 输入
users = {}
flag = True
quit_message = "Please select whether continue(Y/N): "
input_value = ''
while (flag):
    name = input("Input the user name: ")
    age = input("Input the user age: ")
    users[name] = age
    input_value = input(quit_message)
    if input_value.upper() == 'N':
        flag = False
print(users)


# 输出
Input the user name: Tom
Input the user age: 15
Please select whether continue(Y/N): Y
Input the user name: Jerry
Input the user age: 25
Please select whether continue(Y/N): N
{'Tom': '15', 'Jerry': '25'}

猜你喜欢

转载自blog.csdn.net/qq_42957717/article/details/117985488
今日推荐