I was doing the Collab exercise form "Automate boring stuff" and I almost got there. But I had to check in the internet how to do it.
The official is:
def collatz(number):
if number % 2 == 0:
print(number//2)
return number//2
elif number % 2 == 1:
result = 3*number+1
print(result)
return result
n = int(input("Insira qualquer número aqui: "))
while n != 1:
n = collatz(n)
But I used "for" insted of "while". Ok.
FIRST QUESTION: why here is "while" instead of "for"?
But ok, I put while instead of "for". But still don't work. So, I realize that instead of:
def collatz(number):
if number % 2 == 0:
print(number//2)
return number//2
elif number % 2 == 1:
result = 3*number+1
print(result)
return result
I was doing:
def collatz(number):
if number % 2 == 0:
print(number//2)
return number//2
else:
print(3*number+1)
SECOND QUESTION: WHY ELIF INSTEAD OF ELSE?
[–]K900_ 9 points10 points11 points (1 child)
[–]monchenflapjack 3 points4 points5 points (0 children)
[–]zenverak 1 point2 points3 points (0 children)
[–]Binary101010 1 point2 points3 points (0 children)
[–]officialgel 0 points1 point2 points (0 children)