112年關務人員四等程式設計概要
三、下列是以 Python 程式語言撰寫的片段程式,試回答每一小題的輸出結果。每一小題是獨立運作的。注意,若迴圈無法停止,則以無窮迴圈作答。(每小題3分,共30分) (一) total = 0 for i in range(1, 100): total += i print('total = ', total) (二) total = 0 i = 1 while i <= 100: total += i i += 1 print('total = ', total) (三) total = 0 i = 1 while i <= 100: i += 1 total += i print('total = ', total) (四) total = 0 for i in range(2, 100, 2): total += i print('total = ', total) (五) total = 0 i = 1 while i <=100: total += 1 i += 1 print('total = ', total) (六) total = 0 i = 1 while i <= 100: total += i i += 2 print('total = ', total) (七) total = 0 for i in range(100, 1, -1): total += i print('total = ', total) (八) total = 0 i = 100 while i > 0: total += i i -= 1 print('total = ', total) (九)下列的程式,若依序輸入的數值是1、2、3、4、5、6 total = 0 i = 1 while i <= 5: num = eval(input('Enter a number: ')) if num % 5 == 0: break else: total += num i += 1 print('total = ', total) (十)下列的程式,若依序輸入的數值是1、2、3、4、5、6 total = 0 i = 1 while i <= 5: num = eval(input('Enter a number: ')) if num % 5 == 0: continue else: total += num i += 1 print('total = ', total) |
答:
(一)
total = 0 for i in range(1, 100): total += i print('total = ', total) |
執行結果:
total = 4950
說明:
i |
1 |
2 |
3 |
… |
99 |
total |
1 |
3 |
6 |
… |
4950 |
total = 1+2+3+…+99 ==
= 99×50 = 4,950。
(二)
total = 0 i = 1 while i <= 100: total += i i += 1 print('total = ', total) |
執行結果:
total = 5050
說明:
i |
1 |
2 |
3 |
… |
100 |
total |
1 |
3 |
6 |
… |
5050 |
total = 1+2+3+…+100 ==
= 50×101 = 5,050。
(三)
total = 0 i = 1 while i <= 100: i += 1 total += i print('total = ', total) |
執行結果:
total = 5150
說明:
i |
1 |
2 |
3 |
… |
100 |
total |
2 |
4 |
7 |
… |
5150 |
total = 2+3+…+101==
= 50×103 = 5,150。
(四)
total = 0 for i in range(2, 100, 2): total += i print('total = ', total) |
執行結果:
total = 2450
說明:
i |
2 |
4 |
6 |
… |
98 |
total |
2 |
6 |
12 |
… |
2450 |
total = 2+4+…+98 ==
= 49×50 = 2,450。
(五)
total = 0 i = 1 while i <=100: total += 1 i += 1 print('total = ', total) |
執行結果:
total = 100
說明:
i |
1 |
2 |
3 |
… |
100 |
total |
1 |
2 |
3 |
… |
100 |
(六)
total = 0 i = 1 while i <= 100: total += i i += 2 print('total = ', total) |
執行結果:
無窮迴圈
(七)
total = 0 for i in range(100, 1, -1): total += i print('total = ', total) |
執行結果:
total = 5049
說明:
i |
100 |
99 |
98 |
… |
2 |
total |
100 |
199 |
297 |
… |
5049 |
total = 100+99+…+2 ==
= 99×51 = 5,049。
(八)
total = 0 i = 100 while i > 0: total += i i -= 1 print('total = ', total) |
執行結果:
total = 5050
說明:
i |
100 |
99 |
98 |
… |
1 |
total |
100 |
199 |
297 |
… |
5050 |
total = 100+99+98+…+1 ==
= 50×101 = 5,050。
(九)
下列的程式,若依序輸入的數值是1、2、3、4、5、6
total = 0 i = 1 while i <= 5: num = eval(input('Enter a number: ')) if num % 5 == 0: break else: total += num i += 1 print('total = ', total) |
執行結果:
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: 5
total = 10
說明:
i |
1 |
2 |
3 |
4 |
total |
1 |
3 |
6 |
10 |
(十)
下列的程式,若依序輸入的數值是1、2、3、4、5、6
total = 0 i = 1 while i <= 5: num = eval(input('Enter a number: ')) if num % 5 == 0: continue else: total += num i += 1 print('total = ', total) |
執行結果:
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: 5
Enter a number: 6
total = 16
說明:
i |
1 |
2 |
3 |
4 |
6 |
total |
1 |
3 |
6 |
10 |
16 |