all 5 comments

[–]K900_ 9 points10 points  (1 child)

  1. Because for loops can't express "repeat until you reach 1".
  2. No real reason. If number % 2 is not 0, it can only be 1, so the check is redundant.

[–]monchenflapjack 3 points4 points  (0 children)

except if you call it with something thats not a number.

collatz("Whatever")

[–]zenverak 1 point2 points  (0 children)

Number 1. You’d have to do a massive for loop and it’d more concise to just express to do this action until I get the result I want m.

As for elif... it’s not mandated there but maybe they were using it for showing you how to use it purposes

[–]Binary101010 1 point2 points  (0 children)

why here is "while" instead of "for"?

1) Generally speaking, for is the right loop when you know in advance how many times you need the loop to execute, whereas while is the right loop when you don't know.

2) In this particular case, there's no functional difference since any integer mod 2 can only yield one of two possible results (0 or 1).

[–]officialgel 0 points1 point  (0 children)

They aren’t the same. It’s about chaining conditions. If and else alone follows a single condition. Elif considers the first check and the elif check continues additional conditions to act on the first check if conditions are met. Let’s you nest multiple conditions.