Python Forum
Why is the function returning None for a * b instead of number?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is the function returning None for a * b instead of number?
#1
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?
Reply
#2
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(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)))
Reply
#4
@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?
Reply
#5
(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.
omm likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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.
omm likes this post
Reply
#7
(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)
Reply
#8
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.
ndc85430 and omm like this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(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
omm likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#10
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())))
ndc85430 and omm like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why my function is returning None? PauloDAS 6 1,684 Jul-17-2022, 11:17 PM
Last Post: Skaperen
  Checking the number of arguments a function takes Chirumer 3 2,113 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Pausing and returning to function? wallgraffiti 1 2,124 Apr-29-2021, 05:30 PM
Last Post: bowlofred
  is there a single function to confine a number between two others? Skaperen 7 2,762 Nov-28-2020, 06:10 PM
Last Post: Skaperen
  Having hard time understanding the function self-returning itself twice jagasrik 2 2,450 Aug-15-2020, 08:50 PM
Last Post: deanhystad
  RuntimeError: Optimal parameters not found: Number of calls to function has reached m bntayfur 0 6,073 Aug-05-2020, 04:41 PM
Last Post: bntayfur
  basic random number generator in replace function krug123 2 2,007 Jul-31-2020, 01:02 PM
Last Post: deanhystad
  Returning Value from Function with Trackbars vicpylon 3 2,024 May-24-2020, 11:28 PM
Last Post: bowlofred
  Nested Recursive Function not Returning Etotheitau 2 2,218 May-09-2020, 06:09 PM
Last Post: Etotheitau
  Mysql returning number of rows not data AkaAndrew123 4 2,758 Jun-10-2019, 02:31 PM
Last Post: AkaAndrew123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020