Python 循环结构

第1关:While 循环与 break 语句

partcount = int(input())

electric = int(input())

count = 0

#请在此添加代码,当count < partcount时的while循环判断语句

#********** Begin *********#

while(count < partcount):

#********** End **********#

    count += 1

    print("已加工零件个数:",count)

    if(electric):

        print("停电了,停止加工")

        #请在此添加代码,填入break语句

        #********** Begin *********#

        break

        #********** End **********#

第2关:for 循环与 continue 语句

absencenum = int(input())

studentname = []

inputlist = input()

for i in inputlist.split(','):

   result = i

   studentname.append(result)

count = 0

#请在此添加代码,填入循环遍历studentname列表的代码

#********** Begin *********#

for student in studentname :

#********** End **********#

    count += 1

    if(count == absencenum):

        #在下面填入continue语句

        #********** Begin *********#

        continue

        #********** End **********#

    print(student,"的试卷已阅")

第3关:循环嵌套

studentnum = int(input())

#请在此添加代码,填入for循环遍历学生人数的代码

#********** Begin *********#

for student in range(studentnum) :

#********** End **********#

    sum = 0

    subjectscore = []

    inputlist = input()

    for i in inputlist.split(','):

        result = i

        subjectscore.append(result)

    #请在此添加代码,填入for循环遍历学生分数的代码

    #********** Begin *********#

    for score in subjectscore :

    #********** End **********#

        score = int(score)

        sum = sum + score

    print("第%d位同学的总分为:%d" %(student,sum))

第4关:迭代器

List = []

member = input()

for i in member.split(','):

    result = i

    List.append(result)

#请在此添加代码,将List转换为迭代器的代码

#********** Begin *********#

List=iter(List)

#********** End **********#

while True:

    try:

        #请在此添加代码,用next()函数遍历IterList的代码

        #********** Begin *********#

        num=next(List)

        #********** End **********#

        result = int(num) * 2

        print(result)

    except StopIteration:

        break

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值