I want to print and return a value, but cant seem to find my way around it. I have this as of now:
def collatz_number(n):
# If n is not a positive integer
if n<=0:
return "Only positive integers please"
# Even numbers
elif n % 2 == 0:
return n // 2
# If n is 1
elif n == 1:
return 1
# Odd number
elif n % 2 == 1:
return 3*n+1
I want it to function like this:
>>>a = collatz_number(5)
16
>>> print(a)
16
My problem is that I either print 16 two times, if I use print under each if statement (the code would then print the same number the if statement returns), or as it is with this code that you see - I only get 16 after print(a), but not after a = collatz_number(n).
How can I make this code function as I stated above: have it print and return the collatz number of n such that it works like this(as stated above):
>>> a = collatz_number(n)
16
>>> print(a)
16
Im sorry for the bad formulation