How are you suppose to use return in a function?

python

#1

This code is suppose to be a function that returns the outcome in a another file but It returns nothing.

import math

def mean(Number1, Number2, Number3):
    total = Number1 + Number2 + Number3
    return (total / 3)
#original median function
# def lower(a, b):
#     if a < b:
#         return a
#     else:
#         return b

# def higher(a,b):
#     if a > b:
#         return a
#     else:
#         return b
#
# def median(a, b, c):
#     if a > higher(b,c):
#         return higher(b,c)
#     else:
#         if a < lower(b,c):
#             return lower(b,c)
#         else:
#                 return a
#         if a == b or c:
#             return a
#       print(a)

# made it shorter


def median(a, b, c):
    if a <= b <= c or c <= b <= a:
        return(b)
    elif b <= a <= c or c <= a <= b:
        return(a)
    else:
        return(c)



#I couldnt figure out this one.
def rms(n1, n2, n3):
    return (math.sqrt(n1 ** 1/2 + n2 ** 1/2 + n3 ** 1/2 / 2))


# def rms(n1, n2, n3):
#     average = [1,2,3]
#     len(average)



def middle_average(number1, number2, number3):
    average = number1 + number2 + number3
    return (average / 3)

This is the code I enter in a another file

import averages

averages.mean(1, 5, 1)

averages.median(1, 6, 1)

averages.rms(1, 5, 1)

averages.middle_average(1, 5, 1)

but I get no error nothing just

< Process finished with exit code 0 >

What am I doing wrong am I using return incorrectly?


#2

there are two things you can do, you can store whatever the function returns in a variable:

x = averages.mean(1, 5, 1)

and then do further operations with x, or print:

print x

or add a print statement directly to the function call:

print averages.mean(1, 5, 1)

if you use python3, print requires parentheses


#3

I would like to add the following:

In Python 2.X.X, the / operator is an integer division if inputs are integers

>>> 1/2
0
>>> 1.0/2.0
0.5

In Python 3.X.X, the / operator is a float division even if the inputs are integer.

>>> 1/2
0.5

#4

yes, what you specify as python2.7 and python3.3 applies for all python2.x.x and python3.x.x version, not just the one you mention

One addition, in python3 you can use mimic python2 behavior by using // (floor division)


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.