1. Iteration is the word we use to refer to the use of loops in programming. Which of the following is NOT a type of loop. (select from the code examples #1, #2 and #3 below)
2. Repeating identical or similar tasks without making errors is something that computers do well and *most* people do poorly. What is happening in the code below?
3. Code snippet #1 and Code snippet #2 below produce the same output of 1,2,3,4,5. Which statement is correct?
#Code snippet 1
print(1)
print(2)
print(3)
print(4)
print(5)
#Code snippet 2
for i in range(1,6):
print(i)
No, both programs print out 1,2,3,4,5,6
The first produces 1,2,3,4,5 but the second produces a timestable program
Yes. Both produce the same output of 1,2,3,4,5
No, the first outputs 1,2,3,4,5 but the second outputs 1,2,3,4,5,6
4. How is a while loop different from a for loop, with particular reference to line 12 in the while loop below? (note in a for loop, it automatically goes up from 0 to the top range specified, which in this case is 11)
5. In the for loop below, it prints the numbers 1,2,3,4. What one thing do you need to change on line 1to make it print numbers 1,2,3,4,5?
6. What type of loop has been used below and where is the stopping condition for the while loop defined?
name = 'Jonathan'
guess = input("So I'm thinking of person's name. Try to guess it: ")
pos = 0
while guess != name and pos < len(name):
print("Nope, that's not it! Hint: letter ", end='')
print(pos + 1, "is", name[pos] + ". ", end='')
guess = input("Guess again: ")
pos = pos + 1
if pos == len(name) and name != guess:
print("Too bad, you couldn't get it. The name was", name + ".")
else:
print("\nGreat, you got it in", pos + 1, "guesses!")
If Loop. Line 2
While Loop. Line 11
While loop. Line 5
For Loop. Line 5
7. The following program uses a for loop in line 5 to limit the user's tries (to enter their password) to three attempts. Is there a mistake on line 5?
8. What will the following program do?
9. The three types of loops we typically learn about in programming are the For loop, the while loop and the repeat - until loop. In the repeat - until loop, the stopping condition is at the end. In the for loop, the start and stop condition is predefined at
10. In the code below that makes use of a for loop, what would the first two lines of output be, if the user entered the number 3 on line 2.