LOOPS Loops Loops Loops! How can we repeat a piece of code without - - PowerPoint PPT Presentation

loops loops loops loops
SMART_READER_LITE
LIVE PREVIEW

LOOPS Loops Loops Loops! How can we repeat a piece of code without - - PowerPoint PPT Presentation

Session 6 LOOPS Loops Loops Loops! How can we repeat a piece of code without having to write it out over and over?! With loops! The Game Loop For Loop vs. While Loop For Loops While Loops Start/End are Known Start/End not Always Clear


slide-1
SLIDE 1

Session 6

LOOPS

slide-2
SLIDE 2

Loops Loops Loops!

How can we repeat a piece of code without having to write it out over and

  • ver?!

With loops!

slide-3
SLIDE 3

The Game Loop

slide-4
SLIDE 4

For Loop vs. While Loop

For Loops While Loops Start/End are Known Start/End not Always Clear Range “Until” something happens

slide-5
SLIDE 5

For Loops

for i in range(5): print(“I will not chew gum in class.”)

  • What do you think this will print?
  • C/P the first example of loops.pdf into a new

PyCharm file (loops.py) and find out!

slide-6
SLIDE 6

Solution

Output: I will not chew gum in class. I will not chew gum in class. I will not chew gum in class. I will not chew gum in class. I will not chew gum in class.

slide-7
SLIDE 7

Example 2

for i in range(5): print("Please,") print("Can I go to the mall?")

  • What will this print?
  • Predict and test!
slide-8
SLIDE 8

Solution

Please, Please, Please, Please, Please, Can I go to the mall?

slide-9
SLIDE 9

Example 3

for i in range(5): print("Please,") print("Can I go to the mall?")

  • How about this one?
  • Think first, run second!
slide-10
SLIDE 10

Solution

Please, Can I go to the mall? Please, Can I go to the mall? Please, Can I go to the mall? Please, Can I go to the mall? Please, Can I go to the mall?

slide-11
SLIDE 11

Loopy Stuff

For i in range(10) print(i)

  • What number does this loop stop on?
  • (this is a trick question)
slide-12
SLIDE 12

Solution

1 2 3 4 5 6 7 8 9

slide-13
SLIDE 13

Workarounds

#Print the numbers 1 to 10, version 2 for i in range(10): print(i+1) #Print the numbers version 2 for i in range(1,11): print(i)

slide-14
SLIDE 14

Nested Loops

# What does this print? Why? for i in range(3): print("a") for j in range(3): print("b")

  • What will this print?
slide-15
SLIDE 15

Nested Loops

# What does this print? Why? for i in range(3): print("a") for j in range(3): print("b") print("Done")

slide-16
SLIDE 16

Counter Variables, pt. 2

total = 0 for i in range(5): new_number = int(input("Enter a number: " )) total += new_number print("The total is: ", total)

slide-17
SLIDE 17

Example

# What is the value of sum? sum = 0 for i in range(1, 101): sum = sum + i print(sum)

slide-18
SLIDE 18

Extra Loops Practice

# What is the value of a? a = 0 for i in range(10): a = a + 1 print(a)

slide-19
SLIDE 19

Extra Loops Practice

# What is the value of a? a = 0 for i in range(10): a = a + 1 for j in range(10): a = a + 1 print(a)

slide-20
SLIDE 20

Extra Loops Practice

# What is the value of a? a = 0 for i in range(10): a = a + 1 for j in range(10): a = a + 1 print(a)

slide-21
SLIDE 21

While Loops

i = 0 while i < 10: print(i) i = i + 1 for i in range(10): print(i)

slide-22
SLIDE 22

CAUTION

while range(10): print(i) Don’t use range with a while loop! The range function only works with the for

  • loop. Do not use it with the while loop!
slide-23
SLIDE 23

Counter Variables, pt. 3

i = i + 1 → i += 1 i = 0 while i < 10: print(i) i += 1 i = 1 while i <= 2 ** 32: print(i) i *= 2 What does this print?

slide-24
SLIDE 24

Loop Until Quit

done = False while not done: quit = input("Do you want to quit? ") if quit == "y": done = True attack = input("Does your elf attack the dragon? ") if attack == "y": print("Bad choice, you died.") done = True

slide-25
SLIDE 25

Solution

done = False while not done: quit = input("Do you want to quit? ") if quit == "y": done = True elif quit =="n": attack = input("Does your elf attack the dragon? ") if attack == "y": print("Bad choice, you died.") done = True

slide-26
SLIDE 26

Strength-o-Meter

  • Make a new activity file,

strength_o_meter.py, in Session 6

  • Build a while loop to report and increase

player strength

  • Get crazy(should I say loopy?) with it!