Presentation is loading. Please wait.

Presentation is loading. Please wait.

Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.

Similar presentations


Presentation on theme: "Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will."— Presentation transcript:

1 Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will return the string in a different case each time (uppercase, lowercase, swapcase, titlecase, etc.)

2 While loops and for loops
Loop there it is! While loops and for loops

3 While loops While some condition is true, repeat something

4 What’s a while loop Runs while a certain condition is true Examples:
number = int(input("enter a number")) while number !=5: number = int(input(“Guess another number”))

5 Three-Year-Old Simulator
Open the code of the Three-Year Old Simulator in the Chapter 3 Folder in IDLE What is happening?

6 What’s happening?

7 Examining the while Loop
Syntax very similar to if statement : Indent If condition is true, the block is executed Executes block over and over and over until the condition is false

8 Initializing the Sentry Variable
Often, while loops are controlled by a sentry variable Variable used in the condition and compared to some other value or values. What is the sentry variable in the Three-Year- Old Simulator? What is it compared to?

9 Checking the Sentry Variable
Make sure that its possible for the while condition to evaluate to true at some point Otherwise the block will never run Example: response = “Because." while response != "Because.": response = input("Why?\n")

10 Practice What do you think is happening in the following code? Run the code to determine if you were correct! count = 0 while (count < 9): print ('The count is:', count) count = count + 1 print ("Good bye!“)

11 Using else Statement with Loops
Python allows you to have an if and an else statement associated with a loop statement. If the else statement is used with a while loop, the else statement is executed when the condition becomes false.

12 You try it: count = 0 while count < 5: print (count, " is less than 5“) count = count + 1 else: print (count, " is not less than 5“)

13 Avoiding Infinite Loops
Make sure your loop will end at some point Infinite loop – a loop that never stops Example: counter = 0 while counter <= 10: print (counter) What will happen?

14 Creating Intentional Infinite Loops
Are infinite loops always a mistake? Intentional infinite loops – infinite loops with an exit condition built in to the loop body

15 Exiting your loop You might face a situation in which you need to exit a loop completely when an external condition is triggered or there may also be a situation when you want to skip a part of the loop and start next execution. Python provides break and continue statements to handle such situations and to have good control on your loop.

16 The break Statement terminates the current loop
resumes execution at the next statement

17

18 The continue statement
“jump back to the top of the loop” At the top, while condition is tested Loop entered again if it evaluates to true

19

20 Open the Finicky Counter Program in the Chapter 3 Folder
What’s happening in the following code?

21 What’s happening?

22 Finicky Counter Program
Counts from 1 to 10 using an intentional infinite loop. Its finicky because it doesn’t like the number 5 and skips it. Technically the loop goes on forever, unless there is an exit condition from that loop in the loop body. When the loop hits 11, it “breaks out of the loop” and the loop ends. When count = 5, the program does not get to the print(count) statement. Instead it goes right back to the stop of the loop so that 5 is never printed.

23 Hour of Code Review! Log on to https://hourofcode.com/grokeliza
Build a friendly chatbot called "Eliza". Can she fool your friends into thinking she's a human?

24 Independent work Open the Guess My Number file from the chapter 3 folder Read about the guess my number game on pages Complete Challenges # 2 and 3 on page 85

25 Python Loops

26 Other loops Python programming language provides following types of loops to handle looping requirements. Loop type Description While loop Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body. For loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. Nested loop You can use one or more loop inside any another while, for or do..while loop.

27 The for loop Repeats code, but not based on a condition
Repeats part of a program based on a sequence (ordered list of things) Repeats its loop body for each element of the sequence, in order. When it reaches the end of the sequence, it loops again

28 Loopy String Program

29 All sequences are made up of elements
A string is a sequence in which each element is one character

30 If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list.


Download ppt "Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will."

Similar presentations


Ads by Google