Form 4 - Computer

2024-2025

Floor 4 - Computer Room

Mr. Peter

While Loop Accumulator02

Santa Rosa de Lima English Secondary School

聖羅撒英文中學

Colégio de Santa Rosa de Lima - Secção Inglesa

Outline

What is while accumulator in Python

1.

Explore the concept of Accumulator in Looping

How accumulator works in Python

2.

Focusing on the value change of variables in looping

Python accumulator implementation

3.

Define while loop statement for requirements

Accumulator Implementation

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

1

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

1

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

1

2

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

1

2

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

1

2

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

1

2

3

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

1

2

3

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

1

2

3

3

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

1

2

3

3

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

1

2

3

3

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

1

2

3

3

6

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

1

2

3

3

6

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

1

2

3

3

6

4

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

1

2

3

3

6

4

Accumulator Implementaion

1.

3.

while i <= 3 :

4.

Tab

5.

Tab

i = 1

acc = acc + i

i = i + 1

2.

acc = 0

i

_____

acc

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

_____

1

0

1

2

3

3

6

4

End

Python Loop Accumulator Statement Exercises

Python Loop Accumulator - Ex01

Write a Python program using a while loop to calculate the sum of all numbers from 1 to 100 (i.e., 1 + 2 + 3 + ... + 100).

1.

Save your file as "ClassNumber_YourName_Ex01.py"

Use a while loop to iterate through the numbers.

1

Accumulate the sum of all numbers between 1 and 100, inclusive.

2

Print the final sum.

3

Accumulator = \sum_{i=1}^{100} i

The final sum number is 5050

Python Loop Accumulator - Ex02

Write a Python program using a while loop to calculate the total number of apples Alice will have eaten after 30 days, assuming she starts with one apple on the first day and doubles the number each subsequent day (i.e., 1 + 2 + 4 + ... + ?).

1.

Save your file as "ClassNumber_YourName_Ex02.py"

Use a while loop to iterate through the 30 days.

1

Accumulate the total number of apples eaten over the 30 days.

2

Print the final total of apples eaten.

3

Total\ Apples = \sum_{i=1}^{30} 2^{i-1}

Day

_____

Acc

_____

_____

_____

_____

_____

_____

_____

1

1

3

2

7

3

15

4

Apples

_____

_____

_____

_____

1

2

4

8

_____

_____

?

30

_____

?

.

.

.

Python Loop Accumulator - Ex02

Write a Python program using a while loop to calculate the total number of apples Alice will have eaten after 30 days, assuming she starts with one apple on the first day and doubles the number each subsequent day (i.e., 1 + 2 + 4 + ... + ?).

1.

Save your file as "ClassNumber_YourName_Ex02.py"

Use a while loop to iterate through the 30 days.

1

Accumulate the total number of apples eaten over the 30 days.

2

Print the final total of apples eaten.

3

Total\ Apples = \sum_{i=1}^{30} 2^{i-1}

The total number of apples eaten is 1,073,741,823

Day

_____

Acc

_____

_____

_____

_____

_____

_____

_____

1

1

3

2

7

3

15

4

Apples

_____

_____

_____

_____

1

2

4

8

_____

_____

?

30

_____

?

.

.

.

Python Loop Accumulator - Ex03

Write a Python program using a while loop to calculate the total amount Alice will have accumulated after 30 years, assuming she starts with 10,000 dollars and reinvests the principal and interest at 5% annually.

1.

Save your file as "ClassNumber_YourName_Ex03.py"

Use a while loop to iterate through the 30 years.

1

Calculate the total amount accumulated over the 30 years.

2

Print the final total amount accumulated.

3

Total\ Amount = 10000 + \sum_{i=1}^{30} 10000 \times 0.05 \times (1.05)^{i-1}

Year

_____

_____

_____

_____

1

2

3

4

Interest

_____

_____

_____

_____

500

525

551.25

578.81

_____

30

_____

?

.

.

.

Total

_____

_____

_____

_____

10500

11025

11576.25

12155.06

_____

?

Python Loop Accumulator - Ex03

Write a Python program using a while loop to calculate the total amount Alice will have accumulated after 30 years, assuming she starts with 10,000 dollars and reinvests the principal and interest at 5% annually.

1.

Save your file as "ClassNumber_YourName_Ex03.py"

Use a while loop to iterate through the 30 years.

1

Calculate the total amount accumulated over the 30 years.

2

Print the final total amount accumulated.

3

Total\ Amount = 10000 + \sum_{i=1}^{30} 10000 \times 0.05 \times (1.05)^{i-1}

Year

_____

_____

_____

_____

1

2

3

4

Interest

_____

_____

_____

_____

500

525

551.25

578.81

_____

30

_____

?

.

.

.

Total

_____

_____

_____

_____

10500

11025

11576.25

12155.06

_____

?

The total amount of money is $4,3219.42

F4 Lesson05 - Accumulator in Looping02

By Mr Peter

F4 Lesson05 - Accumulator in Looping02

  • 17