Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.
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 :
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
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

what is a peer Group? Importance?

Answered: 1 week ago