Linux 与 Python编程2021 循环结构 educoder实训

第一关

编程要求

本关的编程任务是补全line.py文件中的判断语句部分,具体要求如下:

填入当已处理零件数小于总零件数count < partcount时的while循环判断语句;

在停电时填入break语句跳出循环。

本关涉及的代码文件line.py的代码框架如下:

partcount = int(input())
electric = int(input())
count = 0
请在此添加代码,当count < partcount时的while循环判断语句
########## Begin ##########
########## End ##########
count += 1
print(“已加工零件个数:”,count)
if(electric):
print(“停电了,停止加工”)
# 请在此添加代码,填入break语句
########## Begin ##########
########## End ##########

测试说明

本文的测试文件是line.py,具体测试过程如下:

平台自动编译生成line.exe;

平台运行line.exe,并以标准输入方式提供测试输入;

平台获取line.exe输出,并将其输出与预期输出对比。如果一致则测试通过,否则测试失败。

以下是平台对src/step1/line.py的样例测试集:
测试输入:

3
0
预期输出:

已加工零件个数: 1
已加工零件个数: 2
已加工零件个数: 3
开始你的任务吧,祝你成功!

代码

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 **********#

第二关

编程要求

本关的编程任务是补全checkWork.py文件中的部分代码,具体要求如下:

填入循环遍历studentname列表的代码;

当遍历到缺席学生时,填入continue语句跳过此次循环。

本关涉及的代码文件checkWork.py的代码框架如下:

absencenum = int(input())
studentname = []
inputlist = input()
for i in inputlist.split(’,’):
result = i
studentname.append(result)
count = 0
请在此添加代码,填入循环遍历studentname列表的代码
########## Begin ##########
########## End ##########
count += 1
if(count == absencenum):
# 请在下面填入continue语句
########## Begin ##########
########## End ##########
print(student,“的试卷已阅”)

测试说明

本文的测试文件是checkWork.py,具体测试过程如下:

平台自动编译生成checkWork.exe;

平台运行checkWork.exe,并以标准输入方式提供测试输入;

平台获取checkWork.exe输出,并将其输出与预期输出对比。如果一致则测试通过,否则测试失败。

以下是平台对src/step2/checkWork.py的样例测试集:
测试输入:

2
zhangsan,lisi,wangwu,zhaoliu,qianqi
预期输出:

zhangsan 的试卷已阅
wangwu 的试卷已阅
zhaoliu 的试卷已阅
qianqi 的试卷已阅
开始你的任务吧,祝你成功!

代码

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,"的试卷已阅")

第三关:循环嵌套

编程要求

本关的编程任务是补全sumScore.py文件中的部分代码,具体要求如下:

当输入学生人数后,填入在for循环遍历学生的代码;

当输入各科目的分数后的列表后,填入for循环遍历学生分数的代码。

本关涉及的代码文件sumScore.py的代码框架如下:

studentnum = int(input())
请在此添加代码,填入for循环遍历学生人数的代码
########## Begin ##########
########## End ##########
sum = 0
subjectscore = []
inputlist = input()
for i in inputlist.split(’,’):
result = i
subjectscore.append(result)
# 请在此添加代码,填入for循环遍历学生分数的代码
########## Begin ##########
########## End ##########
score = int(score)
sum = sum + score
print(“第%d位同学的总分为:%d” %(student,sum))

测试说明

本文的测试文件是sumScore.py,具体测试过程如下:

平台自动编译生成sumScore.exe;

平台运行sumScore.exe,并以标准输入方式提供测试输入;

平台获取sumScore.exe输出,并将其输出与预期输出对比。如果一致则测试通过,否则测试失败。

以下是平台对src/step3/sumScore.py的样例测试集:
测试输入:

2
23,56,78,90
21,56,78,76,91
预期输出:

第0位同学的总分为:247
第1位同学的总分为:322
开始你的任务吧,祝你成功!

代码

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))

第四关:迭代器

编程要求

本关的编程任务是补全ListCalculate.py文件中的部分代码,具体要求如下:

当输入一个列表时,填入将列表List转换为迭代器的代码;

填入用next()函数遍历迭代器IterList的代码。

本关涉及的代码文件ListCalculate.py的代码框架如下:

List = []
member = input()
for i in member.split(’,’):
result = i
List.append(result)
请在此添加代码,将List转换为迭代器的代码
########## Begin ##########
########## End ##########
while True:
try:
# 请在此添加代码,用next()函数遍历IterList的代码
########## Begin ##########
########## End ##########
result = int(num) * 2
print(result)
except StopIteration:
break

测试说明

本文的测试文件是ListCalculate.py,具体测试过程如下:

平台自动编译生成ListCalculate.exe;

平台运行ListCalculate.exe,并以标准输入方式提供测试输入;

平台获取ListCalculate.exe输出,并将其输出与预期输出对比。如果一致则测试通过,否则测试失败。

以下是平台对src/step4/ListCalculate.py的样例测试集:
预期输入:

5,6,7,8,9
预期输出:

10
12
14
16
18
开始你的任务吧,祝你成功!

List = []
member = input()
for i in member.split(','):
    result = i
    List.append(result)
#请在此添加代码,将List转换为迭代器的代码
#********** Begin *********#
IterList = iter(List)
#********** End **********#
while True:
    try:
	    #请在此添加代码,用next()函数遍历IterList的代码
        #********** Begin *********#
        num=next(IterList)
        #********** End **********#
        result = int(num) * 2
        print(result)
    except StopIteration:
        break

おすすめ

転載: blog.csdn.net/u014708644/article/details/121212886