1. Any counter-controlled loop (For Loop) can also be implemented with a condition-controlled loop(while loop)
2. The following program asks the user to guess the number, but in this case only allows two guesses and then the program stops. Can you fix the problem to allow up to 10 guesses?
3. The following program, when run with the user entering '2', would produce just one line of output. What would you need to change to get it to print three lines of output, like shown below (times table up with an output of 0,2,4)
def main():
number=int(input("Enter a number : "))
for i in range(0,1):
print(number,"times:", i, "is equal to:", i*number)
main()
(2, 'times:', 0, 'is equal to:', 0)
#Enter a number : 2
#(2, 'times:', 0, 'is equal to:', 0)
#2, 'times:', 1, 'is equal to:', 2)
#(2, 'times:', 2, 'is equal to:', 4)
Change line 4 to: for i in range (0,4)
Change line 4 to: for i in range(0,3)
This cannot be done with a for loop
Change line 4 to: for 2 in range (0,1,4)
4. This program uses a for loop to execute the famous Fibonacci sequence. The output will be:
5. What will the output of the following be?
6. Can you change the code in the for loop below to make it start at 2, stop at 20 and go up by 2 each time. e.g. 2,4,6,8...etc
7. The following program will output: smarties, toblerone, nestle milk. Why does it leave out the last delicious item in the list?
8. Nested loops are used for iterating 2D arrays like the 2d array a below. The below code shows that the first loop iterates through the ___________, the second loop runs through the elements __________ of a row.
9. what will the output of the following code be?
10. A nested loop is a loop within a loop. What is the output of the following?
for x in range(1, 3):
for y in range(1, 3):
print (x, "x", y, "=", x*y)
#Output 1
1 x 1 = 1
1 x 2 = 2
2 x 1 = 2
2 x 2 = 4
#Output 2
(1, x 3, '=', 1)
(1, x 4, '=', 2)
(2, x 5, '=', 2)
(2, x 6, '=', 4)
#Output 3
(1, x 1, '=', 1)
(2, x 2, '=', 4)
(3, x 3, '=', 9)
(4, x 4, '=', 16)
Output 1
Output 2
Output 3
None of these options apply