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?
#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


Messages In This Thread
RE: Why is the function returning None for a * b instead of number? - by perfringo - Oct-28-2020, 01:56 PM

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