Introduction to Python
Floor 4 - Computer Room
Outline
Outline
While Loop
1
Number Sequences
2
Exercises
3
While Loop Statement
1.
2.
Tab
while Condition :
Command1
While Loop Statement
i = 1
while i <= 10:
print(i)
i = i + 1
While Loop from 1 to 10
While loop statement - Ex10_1
Right Triangle
Write a program that asks the user to enter a positive integer n, and then prints a right triangle consisting of n rows of asterisks.
Example
n = int( input( "Please input a positive integer: " ) )
Save the file as "XX_YYYY_Ex10_1.py"
While loop statement - Ex10_2
Inverse Right Tangle
Write a program that asks the user to enter a positive integer n, and then prints a right triangle consisting of n rows of asterisks.
Example
n = int( input( "Please input a positive integer: " ) )
Save the file as "XX_YYYY_Ex10_2.py"
While loop statement - Ex10_3
Multiples of 10:
Write a Python program to print the multiplication table for the number 10 up to 10
Example:
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100
Save the file as "XX_YYYY_Ex10_3.py"
Write a Python program that prints a reversed triangle consisting of 10 rows of asterisks. The first row should contain one asterisk, the second row should contain two asterisks, and so on, up to 10 asterisks in the last row, which should be left-justified.
While loop statement - Ex10_4
Example
* ** *** **** ***** ****** ******* ******** ********* **********
Reversed Triangle
Save the file as "XX_YYYY_Ex10_4.py"
* ** *** **** ***** ****** ******* ******** ********* **********
Reversed Triangle
While loop statement - Ex10_4
* ** *** **** ***** ****** ******* ******** ********* **********
Reversed Triangle
Space
Asterisks
9
_______
1
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
While loop statement - Ex10_4
* ** *** **** ***** ****** ******* ******** ********* **********
Reversed Triangle
Space
Asterisks
9
_______
1
_______
8
_______
2
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
While loop statement - Ex10_4
* ** *** **** ***** ****** ******* ******** ********* **********
Reversed Triangle
Space
Asterisks
9
_______
1
_______
8
_______
2
_______
7
_______
3
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
While loop statement - Ex10_4
* ** *** **** ***** ****** ******* ******** ********* **********
Reversed Triangle
Space
Asterisks
9
_______
1
_______
8
_______
2
_______
7
_______
3
_______
6
_______
4
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
While loop statement - Ex10_4
Reversed Triangle
Space
Asterisks
9
_______
1
_______
8
_______
2
_______
7
_______
3
_______
6
_______
4
_______
5
_______
5
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
_______
* ** *** **** ***** ****** ******* ******** ********* **********
While loop statement - Ex10_4
Reversed Triangle
Space
Asterisks
9
_______
1
_______
8
_______
2
_______
7
_______
3
_______
6
_______
4
_______
5
_______
5
_______
4
_______
6
_______
_______
_______
_______
_______
_______
_______
_______
_______
* ** *** **** ***** ****** ******* ******** ********* **********
While loop statement - Ex10_4
Reversed Triangle
Space
Asterisks
9
_______
1
_______
8
_______
2
_______
7
_______
3
_______
6
_______
4
_______
5
_______
5
_______
4
_______
6
_______
3
_______
7
_______
_______
_______
_______
_______
_______
_______
* ** *** **** ***** ****** ******* ******** ********* **********
While loop statement - Ex10_4
Reversed Triangle
Space
Asterisks
9
_______
1
_______
8
_______
2
_______
7
_______
3
_______
6
_______
4
_______
5
_______
5
_______
4
_______
6
_______
3
_______
7
_______
2
_______
8
_______
_______
_______
_______
_______
* ** *** **** ***** ****** ******* ******** ********* **********
While loop statement - Ex10_4
Reversed Triangle
Space
Asterisks
9
_______
1
_______
8
_______
2
_______
7
_______
3
_______
6
_______
4
_______
5
_______
5
_______
4
_______
6
_______
3
_______
7
_______
2
_______
8
_______
1
_______
9
_______
_______
_______
* ** *** **** ***** ****** ******* ******** ********* **********
While loop statement - Ex10_4
Reversed Triangle
Space
Asterisks
9
_______
1
_______
8
_______
2
_______
7
_______
3
_______
6
_______
4
_______
5
_______
5
_______
4
_______
6
_______
3
_______
7
_______
2
_______
8
_______
1
_______
9
_______
0
_______
10
_______
* ** *** **** ***** ****** ******* ******** ********* **********
While loop statement - Ex10_4
Write a Python program that prints a reversed triangle consisting of 10 rows of asterisks. The first row should contain 10 asterisk, the second row should contain 9 asterisks, and so on, down to 1 asterisks in the last row, which should be right-justified.
While loop statement - Ex10_5
Example
**********
*********
********
*******
******
*****
****
***
**
*
Reversed Triangle
Save the file as "XX_YYYY_Ex10_5.py"