1

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

1
  • The last condition is excessive, you can change the elif into else. Commented Feb 16, 2018 at 16:43

3 Answers 3

4

You almost never want to print from a function. If you don't want the return value printed, there's nothing (easy) you can do about it. If you do want it printed, you can always do so after the function returns the value. If for some reason you must both print and return, save the return value to a variable so that you only need one return statement, which can be preceded by a single call to print.

Unrelated, but don't return strings to indicate errors from functions that otherwise return numbers. Raise an exception instead.

def collatz_number(n):
    if n <= 0:
        raise ValueError("Only positive integers please")

    if n == 1:
        rv = 1
    elif n % 2 == 0:
        rv = n // 2   
    else:
        rv = 3*n+1

    # print(rv)
    return rv

Check for n==1 first, to avoid doing an unnecessary division. Also, a number is either even or odd; if n % 2 != 0, you know it is 1; you don't need to do the division again.

Sign up to request clarification or add additional context in comments.

Comments

0

You can just print the variable inside of the function before returning it.

def foo(x):
    return_value= x+5
    print(return_value)
    return return_value
a=foo(10)

Comments

0

The 'return' statement doesn't print anything, you can only store that returned value into a variable, and then print it. I'm not sure if I understand your question.. but the case is that, if you want to print twice, as you show in your example you have to do this:

def collatz_number(n):
# If n is not a positive integer
    if n<=0:
        print "Only positive integers please"
        return -1 #for example, you could return any other value..


# Even numbers
    elif n % 2 == 0:
        print str(n // 2)
        return n // 2


# If n is 1
    elif n == 1:
        print str(1)
        return 1


# Odd number
    elif n % 2 == 1:
        print str(3*n+1)
        return 3*n+1

observe that to print a number you have to convert it to 'str' type first... So, you cant't do print (a), but instead do print (str(a))

I hope my answer helps! Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.