🌍 All Study Guides📊 Dashboard📰 Blog💡 About
Google IT Automation with Python Professional Certificate • STUDY MODE

WHILE LOOPS

QUESTION 1 OF 5

In Python, what do while loops do?

A
while loops tell the computer to repeatedly execute a set of instructions while a condition is true.Correct Answer
B
while loops instruct the computer to execute a piece of code a set number of times.
C
while loops branch execution based on whether or not a condition is true.
D
while loops initialize variables in Python.
Explanation:

while loops will keep executing the same group of instructions until a specified loop condition stops being true.

QUESTION 2 OF 5

Which techniques can prevent an infinite while loop? Select all that apply.

A
Change the value of a variable used in the while conditionCorrect Answer
B
Use the stop keyword
C
Use the break keywordCorrect Answer
D
Use the continue keyword
Explanation:

If a numeric variable is used to control the iterations of a while condition, that variable must eventually change to a value that makes the while condition False, otherwise the while loop will keep executing indefinitely. Explanation: The break keyword can stop a while loop. It is often used in the body of a nested if-else conditional statement inside of a while loop.

QUESTION 3 OF 5

The following code contains an infinite loop, meaning the Python interpreter does not know when to exit the loop once the task is complete. To solve the problem, you will need to: Find the error in the code Fix the while loop so there is an exit condition Hint: Try running your function with the number 0 as the input and observe the result. Note that Coursera’s code blocks will time out after 5 seconds of running an infinite loop. If you get this timeout error message, it means the infinite loop has not been fixed. ( Image S1Q3 ) Answer: def is_power_of_two(number): # This while loop checks if the "number" can be divided by two # without leaving a remainder. How can you change the while loop to # avoid a Python ZeroDivisionError? while number % 2 == 0: if number == 0: return False number = number / 2 # If after dividing by 2 "number" equals 1, then "number" is a power # of 2. if number == 1: return True return False # Calls to the function print(is_power_of_two(0)) # Should be False print(is_power_of_two(1)) # Should be True print(is_power_of_two(8)) # Should be True print(is_power_of_two(9)) # Should be False Awesome! You fixed a tricky error that was hard to find and the function now behaves correctly.

QUESTION 4 OF 5

Write a function that takes an argument n and returns the sum of integers from 1 to n. For example, if n=5, your function should add up 1+2+3+4+5 and return 15. If n is less than 1, just return 0. Use a while loop to calculate this sum. ( Image S1Q4 ) Answer: def sum_of_integers(n): if n < 1: return 0 i = 1 sum = 0 while i<=n: sum = sum + i i = i + 1 return sum print(sum_of_integers(3)) # should print 6 print(sum_of_integers(4)) # should print 10 print(sum_of_integers(5)) # should print 15 Great work! You)ve written a while loop and gotten Python to do the work for you.

QUESTION 5 OF 5

Fill in the blanks to complete the function, which should output a multiplication table. The function accepts a variable “number” through its parameters. This “number” variable should be multiplied by the numbers 1 through 5, and printed in a format similar to “1x6=6” (“number” x “multiplier” = “result”). The code should also limit the “result” to not exceed 25. To satisfy these conditions, you will need to: Initialize the “multiplier" variable with the starting value Complete the while loop condition Add an exit point for the loop Increment the “multiplier" variable inside the while loop ( Image S1Q5 ) Answer: def multiplication_table(number): # Initialize the appropriate variable multiplier = 1 # Complete the while loop condition. while multiplier<=5: result = number * multiplier if result > 25: # Enter the action to take if the result > 25 break print(str(number) + "x" + str(multiplier) + "=" + str(result)) # Increment the appropriate variable multiplier += 1 multiplication_table(3) # Should print: # 3x1=3 # 3x2=6 # 3x3=9 # 3x4=12 # 3x5=15 multiplication_table(5) # Should print: # 5x1=5 # 5x2=10 # 5x3=15 # 5x4=20 # 5x5=25 multiplication_table(8) # Should print: # 8x1=8 # 8x2=16 # 8x3=24 You completed the multiplication table with all of the required criteria, and it looks great!

Ready to test your recall?

In Python, what do while loops do?

A
while loops tell the computer to repeatedly execute a set of instructions while a condition is true.
B
while loops instruct the computer to execute a piece of code a set number of times.
C
while loops branch execution based on whether or not a condition is true.
D
while loops initialize variables in Python.

How confident are you in this answer?