Python scripting

profileAdejoke
CIS123GuidedPractice2.3.docx

Guided Practice 2.3 – Repetitive structures – For and While Loops

Task 1 – Pseudocode

Many times, your program will need to repeat a number of steps to get the job done. While you could simply copy the statements over and over it is often much easier to use a repetitive structure in your program. There are two repetitive structures, for and while, which we will use to repeat steps in a program.

For the For statement pseudocode we use for and endfor when we know beforehand the number of times you will be repeating the loop. An example

for each month in the year

Statement 1

Statement 2

endfor

In this case Statement 1 and Statement 2 would be repeated for each month of the year. You can also set the number of times the loop will repeat

for month = 1 to 12

Statement 1

Statement 2

endfor

This will also loop through Statement 1 and Statement 2 12 time just as the first loop

You can also put one For statement inside another, which is known as nesting loops.

for year = 2000 to 2010

for month = 1 to 12

Statement 1

Statement 2

endfor

endfor

This statement will loop through each year from 2000 to 2010 and then each month within that year. In this case the program will loop through 132 times (11 years * 12 months each).

The second statement is a while statement which in pseudocode becomes repeat and until. The loop is set and continues until a condition is met. Most programs use a while loop to repeat until something happens, such as the user clicks a button or types in a value.

Repeat

Statement 1

Statement 2

Until user clicks a button

As you can see there is no need to know the number of times the program will loop through the statements before the program ends

First, let’s write a simple program to print out the numbers from 0 to 9. We could simply put the print statements

print(0)

print(1)

print(2)

We could continue to put all 10 statements in the program and it would work fine or we could use the two statements below.

for num in range(10):

print(num)

What if we wanted to write a program to print the number 0 to 9 and then 9 to 0? Let’s change our program by adding a second loop.

for num in range(10):

print(num)

for num2 in range(9, 0, -1):

print(num2)

Write the program with the statements above. Run and take a screenshot of the output.

Be careful of your tabs. What happens if you indent the second loop using a tab so it looks like this?

for num in range(10):

print(num)

for num2 in range(9, 0, -1):

print(num2)

There is also a second looping type that is called a while statement. In this case the program will continue to loop until a condition is met. Let’s change the above program to use a while loop instead of a for loop.

num = 0

while num < 10:

print(num)

num += 1

You will notice we used an index variable (num) which we then need to update each time through the loop. This is why we normally say a for loop is used when you know the number of loops required and a while loop is used when you don’t. A while loop is used when we’re waiting for a condition or action to happen, such as the user clicking a mouse so most program will use a type of while loop instead of a for loop.

Now we are going to write a program to take 5 numbers input from the user and sort and print them out.

Let’s start with the pseudocode

Program SortNumbers:

Initialize variables

Get numbers from user

Loop through until you have 5 numbers

Sort numbers in the list from lowest to highest

Print a number on the list

Loop through all 5 numbers on the list

End.

Let’s put together the program.

Deliverables for Task 1

· Pseudocode for sort numbers program

Task 2 – Sort number program

Now let’s put together the program. Open the Python IDLE program and create a new program called <Your Name>_SortNumbers in this Window

We are going to read the numbers into a special variable called a list. This will allow your list to grow as the user adds numbers to it. So, the first thing we have to do is create the list variable.

At the top of your program enter:

list = []

index = 0

Now let’s enter our first loop:

print(“<Your StudentID>”)

print(“Enter 5 numbers:”)

while index < 5:

list.append(int(input()))

index += 1

print(list)

Text  Description automatically generated

Graphical user interface, text, application  Description automatically generated

We will now add a function called Numbersort(list) to the program. This is a special part of a program that can be called from inside other parts of the program.

Text  Description automatically generated

Finally, we’ll have the second look that will print out the list

print(“Here are the numbers in order”)

for num in range(5):

print(list[num])

Text  Description automatically generated

Take a screenshot of your completed program and another of your output.

Deliverables for Task 2

· Screenshot of your completed program

· Screenshot of the output from your program

Task 3 – Add 1000 numbers to a list

Now instead of 5 numbers we’re going to do the same thing with 100 random numbers in a list. We will add 100 random numbers to a list and then sort and print them out.

First, we need to import the random module so we can generate random numbers

import random

Then we’re going to initialize our variables so the system knows what we’re using them for.

list = []

index = 0

Then we’ll add a loop that will loop through 100 times and append a random number to the list. Use the print(list) command to make sure your program has added the numbers to the list.

print(“<Your StudentID>”)

while index < 1000:

list.append(random.randint(1,100))

index += 1

print(list)

How could you change the program to use 1000 number instead of 100? Change the program to get 1000 random numbers. Run the program again. Could you do the same thing with 10000 numbers? How about 1,000,000? What is the limit for how many numbers you can use in your program?

Next, we’ll sort the list and then print them out again.

list = Numbersort(list)

print(list)

How could you print out the numbers using a loop again? Let’s try it using a for loop.

For index in range(1000):

print(list[index])

This is great unless you want to change the number of items in the list. If so you’ll need to change the number of items in two places in the program. Fortunately, there is a better way. You can change the for loop to:

For item in list:

print(list[item])

With this loop in place, you don’t have to know how many items are in the list before you print them.

Let’s try to rewrite the program so it will work regardless of how many random number are in the list.

import random

list = []

index = 0

print(“<Your StudentID>”)

while index < 1000:

list.append(random.randint(1,100))

index += 1

list = Numbersort(list)

for item in list:

print(list[item])

Change the program to have 1000 random numbers from 1 to 1000 in the list.

Run the program and take a screenshot of the output.

Deliverables for Task 3

Answers for questions about the program

Screenshot of the output of your completed program.

image3.png

image4.png

image1.png

image2.png