Python While Loop Exercise Questions
Below are two exercise questions on Python while loop. We have covered 2 important exercise questions based on the bouncing ball program and the countdown program.
Q1. While loop exercise question based on bouncing ball problem
Python
initial_height = 10 bounce_factor = 0.5 height = initial_height while height > 0.1 : print ( "The ball is at a height of" , height, "meters." ) height * = bounce_factor print ( "The ball has stopped bouncing." ) |
Output
The ball is at a height of 10 meters.
The ball is at a height of 5.0 meters.
The ball is at a height of 2.5 meters.
The ball is at a height of 1.25 meters.
The ball is at a height of 0.625 meters.
The ball is at a height of 0.3125 meters.
The ball is at a height of 0.15625 meters.
The ball has stopped bouncing.
Q2. Simple while-loop exercise code to build countdown clock
Python
countdown = 10 while countdown > 0 : print (countdown) countdown - = 1 print ( "Blast off!" ) |
Output
10
9
8
7
6
5
4
3
2
1
Blast off!
Python While Loop
Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop in the program is executed.
Syntax of while loop in Python
while expression:
statement(s)