Python recursive function returns None instead of value

Horstus

I am just experimenting a little bit with Python and recursive functions.

For now, I wanted to write a function, which splits up a number into 4 pieces until it is smaller than 4.

So, for instance, I feed the function 20, it should then return

[1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25]

I get that result if I print the outcome before the return statement, but cannot get that result if I return the variable which contains these values.

def break_up_number_into_fours(input):
    flag = 0
    input_list = []
    full_result = []
    if isinstance(input, list):
        input_list.extend(input)
    else:
        input_list.append(input)

    for item in input_list:
        if item > 4:
            result = [item/4]*4
            full_result.extend(result)
            flag = 1
        else:
            full_result.append(item)

    if flag == 1:

        break_up_number_into_fours(full_result)
    else:
        print(full_result)
        return full_result

test = break_up_number_into_fours(20)

print(test)

What could be wrong in the code?

Much obliged!

Sahadat Hossain

You did not return any value from your recursive call break_up_number_into_fours(full_result) , you need to do it like:

def break_up_number_into_fours(input):
    flag = 0
    input_list = []
    full_result = []
    if isinstance(input, list):
        input_list.extend(input)
    else:
        input_list.append(input)

    for item in input_list:
        if item > 4:
            result = [item/4]*4
            full_result.extend(result)
            flag = 1
        else:
            full_result.append(item)

    if flag == 1:

        return break_up_number_into_fours(full_result) // here you need to return the value in respective recursive call
    else:
        print(full_result)
        return full_result

test = break_up_number_into_fours(20)

print(test)

Output:

[1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25]

[1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25]

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Object function returns function instead of value

분류에서Dev

Returning a value from a recursive function

분류에서Dev

Python OpenCV Hough Circles returns None

분류에서Dev

data-bind returns function instead of text

분류에서Dev

How to throw an exception from a recursive function in scala that returns Int

분류에서Dev

Is there a python built-in function to get 0 for None

분류에서Dev

Javascript variable that always returns value of function

분류에서Dev

Modular exponentiation function returns the wrong int value

분류에서Dev

Javascript shuffle function returns same value in for loop

분류에서Dev

Django Authenticate returns None

분류에서Dev

Recursive function with timeout and check RxJs Observable value polling

분류에서Dev

Recursive function to find the minimum value in a nested list, without using a for loop

분류에서Dev

Php recursive function return null while variable have value

분류에서Dev

how to pass a value from a recursive function into an array in a different method

분류에서Dev

Return statement in recursive function with try-except structure not executed / returns NoneType

분류에서Dev

user defined function returns error in python

분류에서Dev

Quick Sort Python Recursion - how does the recursive function work

분류에서Dev

How to generically type a function that returns a value array of an object array?

분류에서Dev

Using c++ 20 Concept to check if a function returns a const value

분류에서Dev

How to create public calculation function that returns calculated value?

분류에서Dev

When i run cron for a database function it returns me the same value

분류에서Dev

recursive function building in R

분류에서Dev

Recursive PHP Function

분류에서Dev

argument concatenation in a recursive function

분류에서Dev

Memoise on recursive function

분류에서Dev

Recursive function with IO

분류에서Dev

Python : How to set a variable to something such as "None" or "0" when there is no value from the request

분류에서Dev

Tail Recursive Factorial Function in Scala

분류에서Dev

C recursive function to calculate Factorial

Related 관련 기사

  1. 1

    Object function returns function instead of value

  2. 2

    Returning a value from a recursive function

  3. 3

    Python OpenCV Hough Circles returns None

  4. 4

    data-bind returns function instead of text

  5. 5

    How to throw an exception from a recursive function in scala that returns Int

  6. 6

    Is there a python built-in function to get 0 for None

  7. 7

    Javascript variable that always returns value of function

  8. 8

    Modular exponentiation function returns the wrong int value

  9. 9

    Javascript shuffle function returns same value in for loop

  10. 10

    Django Authenticate returns None

  11. 11

    Recursive function with timeout and check RxJs Observable value polling

  12. 12

    Recursive function to find the minimum value in a nested list, without using a for loop

  13. 13

    Php recursive function return null while variable have value

  14. 14

    how to pass a value from a recursive function into an array in a different method

  15. 15

    Return statement in recursive function with try-except structure not executed / returns NoneType

  16. 16

    user defined function returns error in python

  17. 17

    Quick Sort Python Recursion - how does the recursive function work

  18. 18

    How to generically type a function that returns a value array of an object array?

  19. 19

    Using c++ 20 Concept to check if a function returns a const value

  20. 20

    How to create public calculation function that returns calculated value?

  21. 21

    When i run cron for a database function it returns me the same value

  22. 22

    recursive function building in R

  23. 23

    Recursive PHP Function

  24. 24

    argument concatenation in a recursive function

  25. 25

    Memoise on recursive function

  26. 26

    Recursive function with IO

  27. 27

    Python : How to set a variable to something such as "None" or "0" when there is no value from the request

  28. 28

    Tail Recursive Factorial Function in Scala

  29. 29

    C recursive function to calculate Factorial

뜨겁다태그

보관