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


Messages In This Thread
Why is the function returning None for a * b instead of number? - by omm - Oct-28-2020, 05:31 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why my function is returning None? PauloDAS 6 1,792 Jul-17-2022, 11:17 PM
Last Post: Skaperen
  Checking the number of arguments a function takes Chirumer 3 2,176 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Pausing and returning to function? wallgraffiti 1 2,180 Apr-29-2021, 05:30 PM
Last Post: bowlofred
  is there a single function to confine a number between two others? Skaperen 7 2,869 Nov-28-2020, 06:10 PM
Last Post: Skaperen
  Having hard time understanding the function self-returning itself twice jagasrik 2 2,516 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,166 Aug-05-2020, 04:41 PM
Last Post: bntayfur
  basic random number generator in replace function krug123 2 2,067 Jul-31-2020, 01:02 PM
Last Post: deanhystad
  Returning Value from Function with Trackbars vicpylon 3 2,086 May-24-2020, 11:28 PM
Last Post: bowlofred
  Nested Recursive Function not Returning Etotheitau 2 2,282 May-09-2020, 06:09 PM
Last Post: Etotheitau
  Mysql returning number of rows not data AkaAndrew123 4 2,835 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