loops loops loops loops
play

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


  1. Session 6 LOOPS

  2. Loops Loops Loops! How can we repeat a piece of code without having to write it out over and over?! With loops!

  3. The Game Loop

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

  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!

  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.

  7. Example 2 for i in range(5): print ("Please,") print ("Can I go to the mall?") -What will this print? -Predict and test!

  8. Solution Please, Please, Please, Please, Please, Can I go to the mall?

  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!

  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?

  11. Loopy Stuff For i in range(10) print(i) -What number does this loop stop on? -(this is a trick question)

  12. Solution 0 1 2 3 4 5 6 7 8 9

  13. 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)

  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?

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

  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)

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

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

  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)

  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)

  21. While Loops i = 0 for i in while i < 10: range(10): print (i) print (i) i = i + 1

  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!

  23. 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?

  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

  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

  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!

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend