0% found this document useful (0 votes)
28 views6 pages

Objective 7: Understand Condition Controlled Iterations

The document discusses condition controlled iterations using while loops in Python. It provides examples of using while loops to repeat sections of code until a condition is met, such as repeating user input validation or generating random numbers. It also compares the syntax of a while loop to a for loop. The document suggests practicing with sample problems involving menu selection, guessing games, and other scenarios requiring conditional repetition.

Uploaded by

Regan Darko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views6 pages

Objective 7: Understand Condition Controlled Iterations

The document discusses condition controlled iterations using while loops in Python. It provides examples of using while loops to repeat sections of code until a condition is met, such as repeating user input validation or generating random numbers. It also compares the syntax of a while loop to a for loop. The document suggests practicing with sample problems involving menu selection, guessing games, and other scenarios requiring conditional repetition.

Uploaded by

Regan Darko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Objective 7: Understand

condition
controlled iterations
Objective 7: Understand condition controlled iterations

Iterations & loops

Keyword: while

while is used to repeat a section of code a variable number of times.

while indicates the condition to stop repeating


The indented code underneath is the code to repeat.

Syntax:

choice = int(input("Please select an option in the range 1-3: "))

while ((choice <=1) or (choice >=3)):


choice = int(input("Please select an option in the range 1-3: "))

2
Objective 7: Understand condition controlled iterations

Iterations & loops

We can also use a while loo[ to count the number of times a section of code has run.

import random
ballsOut = 1

while (ballsOut < 8):


print(random.randint(1,50))
ballsOut = ballsOut +1

What does this code do?

3
Objective 7: Understand condition controlled iterations

Iterations & loops

Analysing the program:

import random

ballsOut = 1 Initialise the counting variable

Start loop while (ballsOut < 8): End loop condition


print(random.randint(1,50))
Increment ballsOut = ballsOut +1

4
Objective 7: Understand condition controlled iterations

Iterations & loops

Side by side analysis:

import random import random

ballsOut = 1 for counter in range (1, 7):


ball = random.randint(1,50)
while (ballsOut < 8): print(ball)
print(random.randint(1,50))
ballsOut = ballsOut +1

5
Objective 7: Understand condition controlled iterations

Try the Objective 7: Problems

 Menu selection problem


 Compound interest problem
 Guess the number game problem
 Gamertag problem
 Rock, paper, scissors problem
 Happy numbers problem
 XP problem

You might also like