Question: 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 as the input and observe the result.
Note that Coursera's code blocks will time out after seconds of running an infinite loop. If you get this timeout error message, it means the infinite loop has not been fixed.
def ispoweroftwonumber:
# 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 :
number number
# If after dividing by "number" equals then "number" is a power
# of
if number :
return True
return False
# Calls to the function
printispoweroftwo # Should be False
printispoweroftwo # Should be True
printispoweroftwo # Should be True
printispoweroftwo # Should be False
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
