Python Forum
Recursive function returns None, when True is expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Recursive function returns None, when True is expected
#1
Hi,
I'm learning Python, I guess this must be absolutely silly but it's been driving me mad for hours.

I've simplified a lot from the original problem, and I can't understand how this function returns None in many cases:

def funk(a, b):
    print(a, b)
    if a == (b * 0.10):
        print('flag')
        return True
    elif a > 0.25:
        a -= 0.25
        b -= 1
        funk(a, b)
    else:
        return False
For example with a = 1.25 and b = 8, the output is:
Output:
1.25 8 1.0 7 0.75 6 0.5 5 flag
It displays 'flag' but eventually returns None and not True as I would expect.
However, it returns True for other values like a = 1 and b = 10.

I've made other tests and could never get it returning False.

Why? Thanks for any help to figure it out.

Eventually I've found the solution...

line 9:
return funk(a, b)
Sorry for inconvenience...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  with open context inside of a recursive function billykid999 1 574 May-23-2023, 02:37 AM
Last Post: deanhystad
  function returns dataframe as list harum 2 1,397 Aug-13-2022, 08:27 PM
Last Post: rob101
  function accepts infinite parameters and returns a graph with those values edencthompson 0 855 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,224 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  function that returns a list of dictionaries nostradamus64 2 1,745 May-06-2021, 09:58 PM
Last Post: nostradamus64
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,356 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  Execution of Another Recursive Function muzikman 5 3,000 Dec-04-2020, 08:13 PM
Last Post: snippsat
  Don't Understand Recursive Function muzikman 9 3,668 Dec-03-2020, 05:10 PM
Last Post: muzikman
  list call problem in generator function using iteration and recursive calls postta 1 1,895 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  factorial using recursive function spalisetty06 12 4,051 Aug-25-2020, 03:16 PM
Last Post: spalisetty06

Forum Jump:

User Panel Messages

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