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

Python_A10

This document provides instructions for creating a for loop in Python to repeat actions a specific number of times. It explains how to set up the loop, modify the number of repetitions, and implement a break condition. Additionally, it includes a challenge to create a loop that counts repetitions and displays the output.

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)
6 views

Python_A10

This document provides instructions for creating a for loop in Python to repeat actions a specific number of times. It explains how to set up the loop, modify the number of repetitions, and implement a break condition. Additionally, it includes a challenge to create a loop that counts repetitions and displays the output.

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/ 2

Session 3

Do I Need to Repeat Myself with For Loops?


You are almost ready for your programming mission!

The guessing game you will make needs a loop that repeats itself a
specific number of times. This will be used to set the number of guesses.

Be methodical! Take your time.


Carefully follow instructions.

Follow the instructions to write code that repeats using a for loop. In the previous assignment
you made a while loop, which can repeat instructions forever. A for loop repeats instructions a
specific number of times. It is used when you know how many times they should repeat. This is
programming knowledge you will use often.

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 repeat as the file name. Click Save.

Repeat Instructions a Specific Number of Times

2.  Repeat instructions three times:


for repeat in range(3):
print('I can say this three times.')

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

 From the Run menu, select Run Module.

 Read the text:


I can say this three times.
I can say this three times.
I can say this three times. The loop repeats three times.
>>

 Close the Python Shell.

4.  Change the number of repeats to four:

for repeat in range(4): The number in the bracket


print('I can say this four times.') sets the number of repeats.

 Run the program again. Close the Python Shell.

Copyright © TechnoKids Inc. 50 TechnoPython | Python


Session 3

Break the For Loop


A for loop repeats a specific number of times. You can break the loop if a condition is met. Try it!
5.  Add a condition:

for repeat in range(4):


print('I can say this four times.')
again=input('Should I do it? yes or no? ')
if again=='no':
print('Okay. I will stop.')
break

 Apply your skills to run the program.

 When you see the question you just added, answer yes two times.
I can say this four times.
Should I do it? yes or no? yes
I can say this four times.
Should I do it? yes or no? yes

6.  When you see the question again, type no.

Even though the instructions are set to repeat four


times they can be stopped earlier using break.

 Close the Python Shell.

Change the Word Repeat in the Code


The word repeat in for repeat in range can be anything that makes sense in the code.

7.  Change repeat to question:

for question in range(4):


print('I can say this four times.')
again=input('Should I do it? yes or no? ')
if again=='no':
print('Okay. I will stop.')
break

 Run the program again. Say yes to each question. Close the Python Shell.

Take the Looping Challenge


8. What goes outside and inside the loop? Combine what you know about Python from
previous assignments. Make a loop that says, "Do I need to repeat myself?". Count each
loop. Show the number of times the loop repeats.
Put the code in order and indent some of the lines: Create this output:
Do I need to repeat myself?
print('I repeated myself', number, 'times.')
Do I need to repeat myself?
for challenge in range(5): Do I need to repeat myself?
number=0 Do I need to repeat myself?
number=number+1 Do I need to repeat myself?
print('Do I need to repeat myself?') I repeated myself 5 times.

Close Python

Copyright © TechnoKids Inc. 51 TechnoPython | Python

You might also like