Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition Structures

Similar presentations


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

1 Repetition Structures
A bit more on…

2 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! Only write up the part of the code where the user can keep guessing until they win.

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

4 Practice – IXL Extension
Extend your IXL program so that if the user answers 5 problems correctly in a row, they are considered a “math master!” Print out something that says “You are now a MATH MASTER of …” whatever operator you were testing on

5 Practice: Kgs to Lbs Write a program that lists out a conversion table from kilograms to pounds. Include all values from 1 to 100 for both units. Kilograms Pounds Pounds Kilograms … …

6 Practice: Heads or Tails
Write a program that simulates a coin flip a million times Then, count the # of heads and tails that result from the flips and display your results to the user Also, display the probability of each outcome out of a million tries

7 Sentinels Sometimes, we will need to enter in a large number of items that need to be calculated in a certain way However, we may not know how many values we will be entering We can do a couple of things here: We can ask the user after every single iteration whether they want to add another value (but this is annoying) We can ask the user ahead of time how many values they’re going to input, but they may not know from the start

8 Sentinels A sentinel value is a pre-defined value that the user can type in to indicate that they are finished entering data Example: >> enter a test score (type -1 when done): 100 >> enter a test score (type -1 when done): 80 >> enter a test score (type -1 when done): -1 >> your test average was: 90%

9 Sentinels In this example, “-1” is considered the sentinel. It indicates that the user is finished typing in their data. Sentinels must be distinctive enough that they will not be confused/mistaken as an actual value of data (-1 was a reasonable value in this example because you can’t really get a -1 on a test, unless you’re in Mr. Seok’s class)

10 Practice – Adding Machine
Write a program that asks the user to continually type in integer values Continue to add all the user’s inputted values into a total variable If the user enters “0”, then end the program by displaying the total sum for the user’s values

11 The “break” Command Some of you may have already used this one, but the word “break” signals to Python to immediately end a loop It will not, however, end your program, it only ends the repetition structure and picks up on the next line of code outside of the repetition structure Note that it will terminate the loop immediately, and it will not run code that follows

12 The “break” Command x = 0 while x != 10: if x >= 3: break print (x) x + = 1

13 The While Loop The while loop is sometimes tricky in the sense that you need to set up the condition of the while statement as the negated version of what you’re looking for. For example: x = 0 while x != 10: # you’ll notice here we loop until x = 10 print(x) # the condition is True when x != 10 x += 1

14 The While Loop The break keyword allows us to reverse the logic back to “normal” in the sense that we can keep a while loop running until a given condition is met by the “if” statement This means we can just begin our while loops with this easy statement: while True:

15 The “break” Command x = 0 while True: if x == 3: break print (x) x + = 1

16 The “return” Command We’ve had quite a bit of trouble with this specific keyword. So, I would recommend trying to stay away from it, unless you feel you have a really good understanding of coding, or at least of this specific keyword. It’s worth mentioning that when you use the word “return” inside a loop, which is inside a function, it will do a similar thing to that of the “break,” but it will stop the process of the function all together, not just the loop.

17 The “break” Command

18 The “return” Command

19 Practice – Prime Numbers
Write a program that asks the user to enter any integer Check to see if the integer is prime and print out your result


Download ppt "Repetition Structures"

Similar presentations


Ads by Google