Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition Structures

Similar presentations


Presentation on theme: "Repetition Structures"— Presentation transcript:

1 Repetition Structures
The long awaited …

2 Repetition Structures
I repeat …

3 Repetition Structures
So, by now you have come to realize that our codes can become pretty long, especially when we want to perform a task multiple times Let’s take a look at an example from one of your very own …

4

5 Practice Write a program that a shopping spree!
Give the user a welcome statement that lets them know they have $1,000 to spend at the local mall. Then, ask the user for the price of one item at a time until the user runs out of money. You may also want to program the code to update how much money the user has left after each item Finally, print out the total number of items bought with the $1,000

6 Repetition Structures
You must’ve thought, “There’s gotta be a better way …” and there is! But first, let’s talk about why it SEOKs to use “the old way”: It’s long … (it’s almost like your computer is making you work) More memory, greater space on computer’s hard drive You mess up once, you have to fix it in every repeated step

7 Repetition Structures
The solution is to create a repetition structure, or more commonly called a “loop” This requires two steps: Write the code for the operation you would like to perform Place the code into a special structure that causes Python to repeat it as many times as you would like There are various ways to “loop” in Python, one in which we’ve already seen

8 The Function Loop By now, you should all know how to loop a function

9 The Function Loop When we loop functions, we just need to be careful in what exactly we are asking Python to repeat We saw an example where we wanted to keep prompting a user for a guess of the magic number, but we didn’t want to change the number. So, we were able to accomplish this by choosing the randomly generated number outside of the looped function.

10 The Function Loop Some of you may have noticed that looping functions has it’s drawbacks: Sometimes, it becomes confusing as to which function is being called and which one comes first Local variables are a pain … It can be confusing!

11 Condition Controlled Loops
Today, we are going to learn a new loop, called the “while” loop We call it a “condition-controlled” loop because it is a repetition structure that depends on the fulfillment of a specific condition, similar to the one’s we created in our decision structures In other words, a set of code will be executed for as long as a Boolean expression holds True

12 The “while” Loop The “while” loop works as follows:
Evaluate a Boolean expression If it holds False, skip the block of statements associated with the while loop and continue the program as normal If it holds True: Execute a series of statements At the end of the statement block, re-evaluate the condition of the loop If it is True, repeat If it is False, skip block of statements and continue with program

13 The “while” Loop

14 The “while” Loop

15 The “while” Loop

16 The “while” Loop

17 The “while” Loop

18 The “while” Loop

19 The “while” Loop

20 The “while” Loop

21 The “while” Loop

22 The “while” Loop

23 The “while” Loop

24 The “while” Loop

25 The “while” Loop

26 The “while” Loop

27 The “while” Loop We refer to the process of going through a loop as an “iteration” If a loop cycles 5 times, we say that it has gone through 5 “iterations” The “while” loop is sometimes known as the “pre-test” loop, meaning it only iterates upon the evaluation of True of the Boolean expression This means that you must “set up” your loop before Python will be able to work with it (i.e. setting up a control variable, like “temp”)

28

29 WARNING! There is nothing stopping you from writing a while loop for a condition which may never evaluate to False This will cause what we call an “infinite loop” and will never stop re-iterating This is a huge problem because especially when working on our website, the infinite loop may cause the website to freeze The program should end if you press CTRL+C

30 The Infinite Loop

31 Accumulator Variables
Many programs require you to count or keep track of the total number of iterations that occur We can use an “accumulator variable” to help us do this

32 Accumulator Variables
First, set up the accumulator variable outside of the loop. Generally, they will come right before the repetition structure is entered. Decide on an initial value for your accumulator variable. For most cases, 0 is a good starting place, or maybe even 1. Use a self-referential assignment statement when incrementing an accumulator variable. Example: prize = prize – 10

33 Accumulator Variables
The self-referential assignment statement can be extremely useful and extended to any mathematical operator: A = A + 1 B = B * 2 C = C / 3 D = D – 4

34 Accumulator Variables
However, Python has a shortcut for this They are called “augmented assignment operators”

35 Augmented Assignment Operators
Example Equal to += C += 5 C = C + 5 –= C –= 4 C = C – 4 *= C *= 3 C = C * 3 /= C /= 2 C = C / 2 %= C %= 6 C = C % 6

36 Practice – Christmas Code
Rewrite the Christmas code, this time without functions! (let’s see how much shorter we can make this …) Recall: the user has an opportunity to guess a number between 1 and 10 and if they are right, they win $100. If they are wrong, they get to guess again but their prize money goes down by $10 every time they guess. The user should always win at least $10!

37 Practice – IXL!!!!! Since you all loved IXL soooo much, we are going to recreate the program. Using a single operator, generate random numbers to create practice problems for the given operator. (ex: operator: +, = ?, = ?) Each time, the user gets a problem right, they are awarded a point, and you will print out the number they have gotten right/wrong after each problem.

38 Practice – Elementary School Tool
Write a program that prints out every factor of a given number. Don’t you wish you had this in elementary school …


Download ppt "Repetition Structures"

Similar presentations


Ads by Google