2022年 11月 4日

Python之for循环

1. for循环语句

在这里插入图片描述

for语句的基本形式为:

for iteration_var in sequence:  
  • 1

循环语句依次遍历序列中的成员,执行循环语句。

例如:

list = ['python','java','c','c++']  
for book in list:  
    print("当前书籍为:",book)  
  • 1
  • 2
  • 3

2.continue语句

continue语句的基本形式为:

for iteration_var in sequence:  
    循环语句  
    if 判断语句1continue  
  • 1
  • 2
  • 3
  • 4

当遍历序列时,如果判断语句1为真,则执行continue语句,跳出当前循环,直接进入下一次循环。

例如:

list = ['python','java','c','c++']  
count = 0  
for book in list:  
    count += 1  
    if count == 3:  
        continue  
    print("当前书籍为:",book)  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.编程实例

absencenum = int(input())
studentname = []
inputlist = input()

for i in inputlist.split(','):
   result = i
   studentname.append(result)
count = 0

#循环遍历studentname列表的代码
for student in studentname:
    count += 1
    if(count == absencenum):
        continue   #continue语句  
    print(student,"的试卷已阅")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
测试输入:  
  • 1
2  
zhangsan,lisi,wangwu,zhaoliu,qianqi  
  • 1
  • 2
预期输出:  
  • 1
zhangsan 的试卷已阅  
wangwu 的试卷已阅  
zhaoliu 的试卷已阅  
qianqi 的试卷已阅  
  • 1
  • 2
  • 3
  • 4