# crash course on python week 3 quiz answers

## Week 3 Graded Assessment

#### 21. Fill in the blanks to complete the “odd_numbers” function. This function should return a space-separated string of all odd positive numbers, up to and including the “maximum” variable that's passed into the function. Complete the for loop so that a function call like “odd_numbers(6)” will return the numbers “1 3 5”.def odd_numbers(maximum): return_string = "" # Initializes variable as a string # Complete the for loop with a range that includes all # odd numbers up to and including the "maximum" value. for ___: # Complete the body of the loop by appending the odd number # followed by a space to the "return_string" variable. ___ # This .strip command will remove the final " " space # at the end of the "return_string". return return_string.strip()print(odd_numbers(6)) # Should be 1 3 5print(odd_numbers(10)) # Should be 1 3 5 7 9print(odd_numbers(1)) # Should be 1print(odd_numbers(3)) # Should be 1 3print(odd_numbers(0)) # No numbers displayed

• def odd_numbers(maximum):

return_string = “” # Initializes variable as a string

# Complete the for loop with a range that includes all
# odd numbers up to and including the “maximum” value.
for i inrange(1, maximum+1, 2):

# Complete the body of the loop by appending the odd number
# followed by a space to the “return_string” variable.
return_string += str(i) + ” “

# This .strip command will remove the final ” ” space
# at the end of the “return_string”.
return return_string.strip()

print(odd_numbers(6)) # Should be 1 3 5
print(odd_numbers(10)) # Should be 1 3 5 7 9
print(odd_numbers(1)) # Should be 1
print(odd_numbers(3)) # Should be 1 3
print(odd_numbers(0)) # No numbers displayed

#### 22. The following code raises an error when executed. What's the reason for the error?def decade_counter(): while year < 50: year += 10return year

• Wrong comparison operator
• Failure to initialize the variable
• Incrementing by 10 instead of 1
• Nothing is happening inside the while loop

#### 25. The following code causes an infinite loop. Can you figure out what’s incorrect and how to fix it?def count_to_ten(): # Loop through the numbers from first to last x = 1 while x <= 10: print(x) x = 1count_to_ten()# Should print:# 1# 2# 3 # 4# 5# 6# 7# 8 # 9# 10

• Needs to have parameters passed to the function
• The “x” variable is initialized using the wrong value
• Should use a for loop instead of a while loop
• Variable “x” is assigned the value 1 in every loop

# Shuffle Q/A 1

### Devendra Kumar

Project Management Apprentice at Google