Re: im having problems and need help
Misti Hamon <[email protected]>
| Newsgroups | gmane.comp.python.tutor |
|---|---|
| Message-ID | <CAEnOb6RQxdvTyd8jVf-m0dSJ9A9048mqEvMaQ_ZbMTfSYkN2Hw@mail.gmail.com> |
Your math is incorrect. And as a result, you are running the whole code wrong. To start you out, the % is the modulo operator. Modulo returns the remainder portion of division, so, 5 % 3 would return 2 (3 goes into 5 1 time, with 2 remaining), 5 % 2 would return 1 (2 goes into 5 2 times with remainder 1). On Wed, Sep 20, 2023, 00:05 Robert Towne <[email protected]> wrote: > Im having problems understanding this. Im running the while loop like it > says; and it doesnt complete the loop. > My question is, is it the formula thats wrong or my math. The % sign means > to square the number? > since i the to the second power is 64. Then it should return false. IDK > what I am doing wrong, if you could > help me understand where I am going wrong i would appreciate it thank you. > > 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 > _______________________________________________ > Tutor maillist - [email protected] > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor