0% found this document useful (0 votes)
9 views

Python_A9

This document provides instructions for creating a guessing game using while loops in Python. It emphasizes the importance of being methodical in programming and includes steps for setting up loops, counting iterations, and incorporating user input to control loop execution. The document also suggests enhancements to the game, such as adding delays and allowing players to decide whether to continue counting.

Uploaded by

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

Python_A9

This document provides instructions for creating a guessing game using while loops in Python. It emphasizes the importance of being methodical in programming and includes steps for setting up loops, counting iterations, and incorporating user input to control loop execution. The document also suggests enhancements to the game, such as adding delays and allowing players to decide whether to continue counting.

Uploaded by

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

Session 3

Get Loopy with While Loops


Get ready for another programming mission!
You are going to create a guessing game. The player must pick the correct
number. Clues tell them if their answer is too high or low. Can the player guess
the number before they run out of tries?
To successfully complete the mission, you must be methodical. This means you
are careful and do things in order. It is an important trait in a programmer
because it helps to solve problems. People who are methodical:
• follow a detailed plan or steps
• carefully and precisely do a task in order
• test one thing at a time to find and fix problems
• repeat steps to check results

You need to prepare for the programming mission. Follow the instructions to write code that
starts and stops loops. This is an important skill. Plus, it is a great chance to be methodical.

Open IDLE and Create a New File


1.  Open IDLE (Python).
 From the File menu, select New File.
 From the File menu, select Save.
Type loop as the file name. Click Save.

Loop Instructions Forever

A while loop can repeat instructions forever. It is used when you do not know how many times
the instructions should repeat. A variable can be used to start and stop it.

2.  Make a variable to control the loop:


loop=True

The value True is used to start the loop.

 Start a loop that shows I like to count:


loop=True
while loop:
print('I like to count.')

3.  From the File menu, select Save or press CTRL + S.


 From the Run menu, select Run Module.
 Read the text:
I like to count.
I like to count. The loop will go forever!
I like to count.

 To stop the loop, press CTRL + C on the keyboard. Close the Python Shell.

Copyright © TechnoKids Inc. 47 TechnoPython | Python


Session 3

Count Forever

A variable can be used to count the loops. Try it!


4.  Make a variable to count the loops:
loop=True

count=0
while loop:
print('I like to count.')

 Count the loop:


count=count+1
loop=True
To start, count is 0.
count=0
When the instructions run, the value of
while loop:
the loop is 0+1, which is 1.
count=count+1
When it runs again, the value of loop is
print('I like to count.')
1+1, which is 2.
print(count)

 Apply your skills to run the program. Read the text:


I like to count.
1
I like to count.
2

 To stop the loop, press CTRL + C on the keyboard. Close the Python Shell.

5.  Move code from the loop:


loop=True
count=0

print('I like to count.') Anything in the


while loop: loop is repeated.
count=count+1
print(count)

 Run the program again. Only the numbers repeat:


I like to count.
1
2

 To stop the loop, press CTRL + C on the keyboard. Close the Python Shell.

Slow Things Down

Things are happening too fast. Slow things down to see what is happening.
6.  Import the Time library and add a delay before the loop repeats:
import time
loop=True
count=0
print('I like to count.')
while loop:
count=count+1
print(count)

time.sleep(1)

 Run the program again to see the change. Close the Python Shell.

Copyright © TechnoKids Inc. 48 TechnoPython | Python


Session 3

Stop a While Loop When a Condition is Met

A while loop runs until a condition is met. To stop it, the variable that controls the loop must
change to False.
7.  Stop the loop when count equals 5:
while loop:
count=count+1
print(count)
time.sleep(1)

if count==5: The value False is used to stop the loop.


loop=False
print('I will stop now.')

 Apply your skills to run the program.

 The loop stops. Close the Python Shell.


I like to count.
1
2
3
4
5 To count faster edit sleep:
I will stop now. time.sleep(0.5)
>>

Stop a While Loop if a Player Quits

A while loop is used when you do not know how many times the instructions should repeat. Have
the player decide if they want to stop counting.
8.  Ask the player if they want to stop:
if count==5:
stop=input('Do you want to keep counting? yes or no? ')
if stop=='no':
indent loop=False
4 spaces print('I will stop now.')

 Apply your skills to run the program.


Take the Challenge!
 When you see the question you just added, type no.
Keep asking the player
I like to count. to count after every five
1 numbers.
2
3 HINTS:
4
make an ask variable
5
Do you want to keep counting? yes or no? no ask=0
I will stop now. calculate each time the
>> while loop repeats
ask=ask+1
9.  Run the program again. Answer the question. Type yes.
change if count==5:
The loop will not stop after it counts to 5 if you answer to if ask==5:
yes because the question never gets asked again.
Take the challenge to improve the code. set ask to zero to restart
the loop if the player
 Use your skills to close the Python Shell. answers yes
ask=0
Close Python

Copyright © TechnoKids Inc. 49 TechnoPython | Python

You might also like