I'm attempting the first project from a book I'm trying to learn Python from.
The task was to write a function called 'collatz', with a parameter named 'number'. if number is even, then collatz should print number // 2 and return this value.
If odd then collatz should print and return 3 * number + 1.
Then the task was to write a program that lets the user input an integer, so that collatz keeps getting called with the entered argument, until the return value is 1.
I know I've printed in the loop, rather than the function, but other than that, does it seem ok?
# The Collatz Sequence
def collatz(number):
if number % 2 == 0:
return (number // 2)
elif number % 2 == 1:
return (3 * number + 1)
ans = ''
while ans != 1:
print('Enter an integer')
r = int(input())
ans = collatz®
print(ans)



Back to top







