Instead of printing the numbers in the sequence it should collect them in a list and return the list. Here is the code that prints numbers in the sequence:
def collatz_sequence(n):
while n > 1:
print(n)
if n % 2 == 1:
n = 3*n + 1
else:
n = n // 2
Use
list.append:Here is a note:
Since
nis an integer,n % 2can only equal to1or0, hencecan be simplified to