Python Forum
Why is the function returning None for a * b instead of number? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why is the function returning None for a * b instead of number? (/thread-30611.html)

Pages: 1 2


Why is the function returning None for a * b instead of number? - omm - Oct-28-2020

Problem: Write calculations using functions and get the results. Let's have a look at some examples:
seven(times(five())) # must return 35


This small function is working as expected and returning 7 * 5 as 35 but
a = 0
b = 0
sign = ''

def op(a, sign ,b):
   if sign == '*':
      return a * b 

print(op(5, '*', 7)) # Working fine with result 35
When I am implementing the similar concept in a slightly bigger program, why is it returning None instead of 35?
a = 0
b = 0
sign = ''


def op(a, sign, b):
    print(a, sign, b)

    if sign == '*':
        print(a * b)
        return a * b


def seven(fun):
    global a
    a = 7
    op(a, sign, b)


def times(fun):
    global sign
    sign = '*'

def five():
    global b
    b = 5


print(seven(times(five()))) # expecting  35 but getting None
Also, am I not using the global keyword properly?


RE: Why is the function returning None for a * b instead of number? - perfringo - Oct-28-2020

Functions without return or yield are returning None.

One possible solution could be that five returns 5, times returns mulitple operator and integer provided as argument and seven uses times function results to multiple with 7.


RE: Why is the function returning None for a * b instead of number? - omm - Oct-28-2020

(Oct-28-2020, 05:31 AM)omm Wrote: Problem: Write calculations using functions and get the results. Let's have a look at some examples:
seven(times(five())) # must return 35


This small function is working as expected and returning 7 * 5 as 35 but
a = 0
b = 0
sign = ''

def op(a, sign ,b):
   if sign == '*':
      return a * b 

print(op(5, '*', 7)) # Working fine with result 35
When I am implementing the similar concept in a slightly bigger program, why is it returning None instead of 35?
a = 0
b = 0
sign = ''


def op(a, sign, b):
    print(a, sign, b)

    if sign == '*':
        print(a * b)
        return a * b


def seven(fun):
    global a
    a = 7
    op(a, sign, b)


def times(fun):
    global sign
    sign = '*'

def five():
    global b
    b = 5


print(seven(times(five()))) # expecting  35 but getting None
Also, am I not using the global keyword properly?

(Oct-28-2020, 06:10 AM)perfringo Wrote: Functions without return or yield are returning None.

One possible solution could be that five returns 5, times returns mulitple operator and integer provided as argument and seven uses times function results to multiple with 7.

My code is working if I write the whole calculation logic in return statement of function seven() like below. But still wondering, why I am not able to pass it to another function
I have implemented the remaining code of all 4 operations (*,+,_,/)
 
def seven(fun):
    global a
    a = 7
   # op(a, sign, b)
    return a * b if sign == '*' else (int(a/b) if sign == '/' else (a+b if sign == '+' else (a-b if sign == '-' else a)))



RE: Why is the function returning None for a * b instead of number? - omm - Oct-28-2020

@perfringo
Even without return statements of five, seven and times etc, the op function was still able to fetch the values of a, sign, b and able to print values until before return statement. But why is the return statement of op returning None instead of a*b which is 35?


RE: Why is the function returning None for a * b instead of number? - buran - Oct-28-2020

(Oct-28-2020, 10:49 AM)omm Wrote: But why is the return statement of op returning None instead of a*b which is 35?
(Oct-28-2020, 06:10 AM)perfringo Wrote: Functions without return or yield are returning None.



RE: Why is the function returning None for a * b instead of number? - jefsummers - Oct-28-2020

Your print statement prints the result of function seven. If seven does not return anything, print will print none. Seven may call op and op will return values to seven, but if seven does not pass them on to print, you don't get your desired result.


RE: Why is the function returning None for a * b instead of number? - omm - Oct-28-2020

(Oct-28-2020, 11:01 AM)jefsummers Wrote: Your print statement prints the result of function seven. If seven does not return anything, print will print none. Seven may call op and op will return values to seven, but if seven does not pass them on to print, you don't get your desired result.

Gotcha. Thanks. So for seven(), this would do..
def seven(fun):
    global a
    a = 7
    return op(a, sign, b)



RE: Why is the function returning None for a * b instead of number? - buran - Oct-28-2020

I have a question - is this your interpretation of the assignment?
(Oct-28-2020, 05:31 AM)omm Wrote: Problem: Write calculations using functions and get the results.
because mine would be a bit different, something within these lines:

def add(a, b):
    return a + b

print(add(3, 5))
or maybe using operator module.


RE: Why is the function returning None for a * b instead of number? - perfringo - Oct-28-2020

(Oct-28-2020, 05:31 AM)omm Wrote: Let's have a look at some examples:
seven(times(five())) # must return 35

If this is all what is required then I would approach it in simple way.

I observe that five() is argument of function times() and function seven() argument(s) is/are result of function times.

So - five() should return 5. Simple enough:

def five():
    return 5
Now we have to think what should return function times(). It takes integer as input and this should be returned, otherwise we have nothing to calculate. We also should pass operator, otherwise we have no operation to perform. As this is probably homework I will go for operator module:

import operator

def times(num):
    return operator.mul, num 
Now we have to write function seven() which takes times() as argument. Our function returns operator and integer, we just unpack and calculate:

def seven(func):
    multiple, num = func 
    return multiple(7, num)
Now I can call as in example and get required result:

print(seven(times(five()))) -> 35
As times() expects number one can provide it directly, without calling function:

print(seven(times(5))) -> 35
print(seven(times(7))) -> 49



RE: Why is the function returning None for a * b instead of number? - deanhystad - Oct-28-2020

Why are you trying to do this horrible thing? If you are trying to understand how functions work I think an example like this is only going to miseducate you. If you are trying to figure out the solution to some problem I think your approach here has to be wrong.

That out of the way, and assuming there is a reason for doing this horrible thing, this is how I would do it.
import operator

def number(a, op):
    if op:
        a = op[0](a, op[1])
    return a

def five(op=None):
    return number(5, op)

def seven(op=None):
    return number(7, op)

def plus(a):
    return (operator.add, a)

def times(a):
    return (operator.mul, a)

print(five(times(seven())))
print(seven(times(five())))