28. Fill in the blanks to complete the “even_numbers” function. This function should return a space-separated string of all positive even numbers, excluding 0, up to and including the “maximum” variable that's passed into the function. Complete the for loop so that a function call like “even_numbers(6)” will return the numbers “2 4 6”.
29. What happens when the Python interpreter executes a loop where a variable used inside the loop is not initialized?
- Nothing will happen
- Will produce a NameError stating the variable is not defined
- The variable will be auto-assigned a default value of 0
- Will produce a TypeError
30. What is the final value of “x” at the end of this for loop? Your answer should be only one number.
for x in range(1, 10, 3): print(x)
for x in range(1, 10, 3):
- 7
32. Find and correct the error in the for loop below. The loop should check each number from 1 to 5 and identify if the number is odd or even.
33. The following code is supposed to add together all numbers from x to 10. The code is returning an incorrect answer, what is the reason for this?
x = 1
sum = 5
while x <= 10:
sum += x
x += 1
print(sum)
# Should print 55
- Should use a for loop instead of a while loop
- The code is not inside of a function
- Not incrementing the iterator (x)
- The “sum” variable is initialized with the wrong value
34. The following code causes an infinite loop. Can you figure out what is incorrect?
def test_code(num): x = num while x % 2 == 0: x = x / 2
test_code(0)
def test_code(num):
x = num
while x % 2 == 0:
x = x / 2
- When called with 0, it triggers an infinite loop
- The modulo operator is used incorrectly
- Missing an else statement
- Missing the continue keyword