Educoder Python入門之控制結構 - 循環結構

題目鏈接:https://www.educoder.net/tasks/wj2s498hzpbe

第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 *********#
IterList = iter(List)
#********** End **********#
while True:
    try:
        #請在此添加代碼,用next()函數遍歷IterList的代碼
        #********** Begin *********#
        num = next(IterList)
        #********** End **********#
        result = int(num) * 2
        print(result)
    except StopIteration:
        break
    

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章