Introduction to Python

Form 4 - Computer

2023-2024

Floor 4 - Computer Room

Mr. Peter

Outline

Outline

While Loop

1

Number Sequences

2

Exercises

3

While Loop Statement

1.

2.

Tab

while Condition :

Command1

Same

Differrent

1.

2.

While loop statement - Ex13_1

Save the file as "XX_YYYY_Ex13_1.py"

Summing the Digits of a Number

Write a Python program that asks the user to input a number and then calculates the sum of its digits.

1.

Prompt the user to enter a number.

2.

Extract each digit from the number entered by the user.

3.

4.

Sum all the digits of the number.

Print the result of the sum

Input:
123

Output:
6

Example

Same

Differrent

1.

2.

While loop statement - Ex13_2

Save the file as "XX_YYYY_Ex13_2.py"

Count the Number of Twos in an Input Number

Create a Python script to count the occurrences of the digit "2" in a user-provided integer. Display the count to the user.

1.

Prompt the user to enter a number.

2.

Iterate through each digit of the number.

3.

4.

Count how many times the digit "2" appears.

Print the counting number.

Input:
2024

Output:
2

Example

Same

Differrent

1.

2.

While loop statement - Ex13_3

Save the file as "XX_YYYY_Ex13_3.py"

The Largest Digit of the Entered Number

Create a Python script to find the maximum digit of a number entered by a user.

1.

Prompt the user to enter a number.

2.

Iterate through each digit of the number.

3.

4.

Find the largest digit of the entered number.

Print the largest number.

Input:
2024

Output:
4

Example

Same

Differrent

1.

2.

While loop statement - Ex13_4

Save the file as "XX_YYYY_Ex13_4.py"

The Second Largest Digit of the Entered Number

Create a Python script to find the second largest digit of a number entered by a user.

1.

Prompt the user to enter a number.

2.

Iterate through each digit of the number.

3.

4.

Find the second largest digit of the entered number.

Print the second largest number.

Input:
20391

Output:
3

Example

Python - While Loop

By Mr Peter

Python - While Loop

  • 261