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 Range “Until” something happens
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!
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.
Example 2 for i in range(5): print ("Please,") print ("Can I go to the mall?") -What will this print? -Predict and test!
Solution Please, Please, Please, Please, Please, Can I go to the mall?
Example 3 for i in range(5): print ("Please,") print ("Can I go to the mall?") -How about this one? -Think first, run second!
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?
Loopy Stuff For i in range(10) print(i) -What number does this loop stop on? -(this is a trick question)
Solution 0 1 2 3 4 5 6 7 8 9
Workarounds #Print the numbers #Print the version 2 numbers 1 to 10, version 2 for i in range(1,11): for i in range(10): print(i) print(i+1)
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?
Nested Loops # What does this print? Why? for i in range(3): print ("a") for j in range(3): print ("b") print ("Done")
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)
Example # What is the value of sum? sum = 0 for i in range(1, 101): sum = sum + i print (sum)
Extra Loops Practice # What is the value of a? a = 0 for i in range(10): a = a + 1 print (a)
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)
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)
While Loops i = 0 for i in while i < 10: range(10): print (i) print (i) i = i + 1
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!
Counter Variables, pt. 3 i = 1 i = i + 1 → i += 1 while i <= 2 ** i = 0 32: while i < 10: print (i) print (i) i *= 2 i += 1 What does this print?
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
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
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!
Recommend
More recommend