crash course on python week 3 quiz answers

crash course on python week 3 quiz answers

Week 3 Graded Assessment

16. Fill in the blanks to print the numbers 1 through 7.

17. Find and correct the error in the for loop. The loop should print every number from 5 to 0 in descending order.

18. Fill in the blanks to complete the “factorial” function. This function will accept an integer variable “n” through the function parameters and produce the factorials of this number (by multiplying this value by every number less than the original number [n*(n-1)], excluding 0). To do this, the function should:

accept an integer variable “n” through the function parameters;

initialize a variable “result” to the value of the “n” variable;

iterate over the values of “n” using a while loop until “n” is equal to 0;

starting at n-1, multiply the result by the current “n” value;

decrement “n” by -1.

For example, factorial 3 would return the value of 3*2*1, which would be 6.

19. Fill in the blanks to complete the “multiplication_table” function. This function should print a multiplication table, where each number is the result of multiplying the first number of its row by the number at the top of its column. Complete the range sequences in the nested loops so that “multiplication_table(1, 3)” will print:

1 2 3

2 4 6

3 6 9

20. Fill in the blanks to complete the “countdown” function. This function should begin at the “start” variable, which is an integer that is passed to the function, and count down to 0. Complete the code so that a function call like “countdown(2)” will return the numbers “2,1,0”.

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 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

  • 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 += 10
return year

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

23. What is the first number that will be printed in the first iteration of this loop? Your answer should be only one number.

for count in range(1, 6):
print(count+1)

24. What is the final value of "y" at the end of the following nested loop code? Your answer should be only one number.

for x in range(10):
for y in range(x):
print(y)

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 = 1


count_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

26. Fill in the blanks to print the numbers from 15 to 5, counting down by fives.

27. Fill in the blanks to complete the function “digits(n)” to count how many digits the given number has. For example: 25 has 2 digits and 144 has 3 digits.

Tip: you can count the digits of a number by dividing it by 10 once per digit until there are no digits left.

Devendra Kumar

Project Management Apprentice at Google

Leave a Reply