0% found this document useful (0 votes)
19 views12 pages

While Loop - Python

while loop python

Uploaded by

mickoleplays
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
19 views12 pages

While Loop - Python

while loop python

Uploaded by

mickoleplays
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 12

Lesson Review

1. What are loops and how they


are used in programming?
2. What is a ‘for’ loop?
Objectives
• Distinguish between loops that repeat a fixed number of times and
loops that repeat as long as a condition is true.
• Use a while loop to create programs.
• Analyze and solve the given problems with creativity.
• Collaborate with your partner in the given activity.
Game Mechanics
1. The game generates a random number between 1 and 20.
2. The player is prompted to guess the number.
3. The player receives feedback on whether their guess is too
low, too high, or correct.
4. The game continues until the player guesses the correct
number, and the total number of attempts is displayed.
‘While’ Loop
is used to repeat a specific
block of code an unknown
number of times.
Uses of ‘while’ loop
1. Automating Repetitive Tasks
2. Managing Tasks Until Completion
3. Continuous Monitoring
4. Iterative Problem-Solving
Structure of a ‘while’ loop
while condition:
#code to repeat
• while: Indicates the start of a while loop.
• condition: A test that determines whether the loop should
continue.
• code to repeat: The indented block of code that executes
repeatedly as long as the condition is true.
Code:
count = 1
while count <=5:
print(“This is loop number:”, count)
count +=1
Real-World Analogy
Activity (20 minutes)
Objective: Write a Python program that prints the numbers from 1 to 10 using while
loop.

Direction:
1.Start by setting a variable, ‘count’, to 1.
2.Use a while loop that continues as long as ‘count’ is less than or equal to 10.
3.Inside the loop, use the print function to display the value of ‘count’. After
printing, increase the value of ‘count’ by 1 to move to the next number.
4.The loop automatically stops when ‘count’ exceeds 10.
Assignment: Guess the number
Create a number guessing game in Python that:
• Generates a random number between 1 and 20.
• Prompts the user to guess the number.
• Provides feedback if the guessed number is too high or too low.
• Continues prompting until the correct number is guessed.
• Prints a congratulatory message with the number of attempts once guessed correctly.
Requirements:
• Use a while loop for continuous prompting.
• Implement input validation for valid number inputs within the range.
• Display appropriate messages to guide the user.
Hints:
• Utilize Python's random module for generating the number.
• Use input() for user input and int() to convert input into an integer.
Submission: Submit your Python script (.py file) containing the game implementation and any additional comments
explaining your code.

You might also like