Preview

2 - Repeating with Loops and Lists

 1. Try changing the list on line 5 to : number_list = [1,2,3,4,5,6,7,8]. Note the difference. This is the list that Tina gets her moving numbers from on lines 8 and 9. Line 8 and Line 13 are the beginning of what is called a:

  Selection statement (IF)

  movement statement

  Snake statement

  For Loop

 2. In the following code on line 9 print(number*10) prints out the numbers 10,20,30,40....to 80. Why does it stop at 80?

  Because there are a maximum of 8 numbers allowed for any given shape

  Because the loop is referring the list "number_list = [1,2,3,4,5,6,7,8]" and multiplying each number in the list by 10 (and it goes up to 8)

  I have no idea

  Because there are 8 coordinates on any given grid in Python

 3. The following also uses a for loop and the same logic to produce a sequence of text in black. How would you get it to produce the text in different colours/colors?

  Change line 11 to tina.color("green")

  It cannot be done

  Change the list on line 5 to: color_list = ["black", "blue","red","green"]

  Change line 7 tina.left(90) to tina.changecolor(colors)

 4. This is a simple for loop that prints the numbers 0 to 9. What would you need to change about the code to make it print 0,10,20,30,40,50,60,70,80,90?

  Change line 2 to: print(i*10)

  It cannot be done

  Change line 1 to: for i in range(10 to 90)

  Change line 2 to: print(0,10,20,30,40,50,60,70,80,90)

 5. In computer science, a loop is a programming structure that _________________________________________________Programmers use loops to cycle through values, add sums of numbers, repeat functions, and many other things. e.g while & for loops

  asks the question 'if' and uses the term 'else'

  None of the above

  lists a group of items forever

  repeats a sequence of instructions until a specific condition is met.

 6. A fancy name for loops is 'iteration' - a fundamental and important concept in computing. How would loops be used in game design?
angrybirdspythonloop.jpg

  Loops are like logic questions - they use the terms 'if' and 'else'

  Loops are only useful in producing numbers so would not help in game programming

  Loops would not be useful in game design as they aren't complex enough

  Like all loops, "for loops" execute blocks of code over and over again which is an incredibly useful feature

 7. In the code below line 9 prints the value of 'angle' to the screen as: 51.4285714286. What is angle and how did it get this value?

  angle is a variable and it gets its value from line 8 where angle is divided by the number of circles drawn on the screen. This gives a value of 51.4285714286

  angle is a variable and it gets its value from line 8 where angle = 360 / 7 = 51.4285714286. 7 is the length of the list called colors

  angle is a type of for loop. The 51.4285714286 refers to the specific coordinates on the grid.

  angle is a list and when the list length is divided by 360 it gives a coordinate which can be used

 8. What would happen if you change line 6 from for number in range(10): to for number in range(100): ?
import turtle
tina = turtle.Turtle()
tina.shape('turtle')

tina.color("green") 
for number in range(10):
    tina.forward(number) 
    

  It would print 100 lines one after the other.

  It would cause an error

  It would draw on a greater range of numbers and therefore draw a vertical line instead of a horizontal one

  It would draw on a greater range of numbers and therefore draw a longer horizontal line

 9. Do both code snippets produce the same result? Which is more efficient and why?
#Code snippet 1
=====================================
import turtle
tina = turtle.Turtle()
tina.shape('turtle')

tina.left(90)
tina.forward(20)
tina.write("What color am I now?")

tina.forward(20)
tina.color("blue")
tina.write("What color am I now?")

tina.forward(20)
tina.color("purple")
tina.write("What color am I now?")

tina.forward(20)
tina.color("green")
tina.write("What color am I now?")

#Code snippet 2
=====================================
import turtle
tina = turtle.Turtle()
tina.shape('turtle')

color_list = ["black", "blue", "purple", "green"]

tina.left(90)

for color in color_list:
    tina.forward(20)
    tina.color(color)
    tina.write("What color am I now?")

  Yes they do produce the same result, but code snippet #1 is more efficient as it contains more code

  No they do not produce the same result

  Yes they do produce the same result, but code snippet #2 is more efficient because it uses better programming grammar and has no mistakes

  Yes they do produce the same result, but code snippet #2 is more efficient as it uses a loop to avoid repeating code again and again

 10. This code draws an incomplete circle. What does line 5 do and what does the number inside the for loop statement in this line mean? Try to change the number to produce a full circle.

  Line 5 is the for loop that decides how many times to repeat the instructions on line 6 and 7 (which are inside the loop). Increasing the number 60 to say 600 would draw a full circle

  Line 5 is the line that stops the loop from repeating and 60 refers to the number of dots in the shape

  Line 5 is the list which decides how many numbers to count up to

  Line 5 is the for loop and the number 60 needs to be decreased in order for it to draw a circle. 60 refers to the number of seconds in a minute.