Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition Structures The long awaited …. Repetition Structures I repeat …

Similar presentations


Presentation on theme: "Repetition Structures The long awaited …. Repetition Structures I repeat …"— 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 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

6 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

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

8 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.

9 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!

10 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

11 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

12 The “while” Loop

13

14

15

16

17

18

19

20

21

22

23

24

25

26 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”)

27

28 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

29 The Infinite Loop

30 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

31 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

32 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

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

34 Augmented Assignment Operators Augmented Assignment Operator ExampleEqual to += C += 5C = C + 5 –= C –= 4C = C – 4 *= C *= 3C = C * 3 /= C /= 2C = C / 2 %= C %= 6C = C % 6

35 Practice – Trick or TREAT Rewrite the TREAT problem from the Halloween quiz, 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!

36 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: +, 4 + 3 = ?, 5 + 7 = ?) 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.

37 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 The long awaited …. Repetition Structures I repeat …"

Similar presentations


Ads by Google