Fwd: im having problems and need help
Leam Hall <[email protected]>
| Newsgroups | gmane.comp.python.tutor |
|---|---|
| Message-ID | <[email protected]> |
Hey Robert, no worries! If I am using whole numbers for division, like 3, and 20, then: 20 divided by 3 gives 6. 3 times 6 is 18, and if I am dividing into 20, there is 2 left over. So, 20 % 3 is 2, since there is 2 left over. When you say: if number % 2 == 0: You have already proven that the number is even. Does that help? Leam -------- Forwarded Message -------- Subject: Re: [Tutor] im having problems and need help Date: Wed, 20 Sep 2023 12:03:12 -0700 From: Robert Towne <[email protected]> To: Leam Hall <[email protected]> I'm lost on the modelo. % it's a reminder? I don't understand it. I ran it in print(number%2). No matter what the number was it would come out to 1 or 0. I'm thinking the % is like a pre program. If it's even or odd. I'm not sure. Though. I'm stuck. I can run a while loop. But I'm lost at this modulo %. On Wed, Sep 20, 2023, 4:48 AM Leam Hall <[email protected] <mailto:[email protected]>> wrote: Robert, Sorry, your mail program killed the code indents. Also, "%" is the modulus/modulo operator, not the power. >>> 20 % 2 0 So, the while line: number % 2 == 0 Returns True if number is even. It says "If number / 2 leaves no remainder". Compare that to: >>> 20 % 3 2 Which says "20 / 3 leaves a remainder of 2". Hope that helps. Leam On 9/15/23 15:07, Robert Towne 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] <mailto:[email protected]> > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor <https://mail.python.org/mailman/listinfo/tutor> -- Software Engineer (reuel.net/resume <http://reuel.net/resume>) Scribe: The Domici War (domiciwar.net <http://domiciwar.net>) General Ne'er-do-well (github.com/LeamHall <http://github.com/LeamHall>) _______________________________________________ Tutor maillist - [email protected] <mailto:[email protected]> To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor <https://mail.python.org/mailman/listinfo/tutor> -- Software Engineer (reuel.net/resume) Scribe: The Domici War (domiciwar.net) General Ne'er-do-well (github.com/LeamHall) _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor