0

Im new in Python and i am try to do recursion. why this return None, but when I return print it, it has a value

my_list = [30, 40, 50, 1000, 20, 60, 90, 100]
print(largest(my_list))


def largest(my_list):
    if len(my_list) > 1:
        x = my_list[len(my_list)-1]
        if x is max(my_list):
            return x
        my_list.pop()
        largest(my_list)

The code above will print None, but if I change the return function to print(x) instead of return x, like the code below:

my_list = [30, 40, 50, 1000, 20, 60, 90, 100]
largest(my_list) #This is the change


def largest(my_list):
    if len(my_list) > 1:
        x = my_list[len(my_list)-1]
        if x is max(my_list):
            return print(x) #This is the change
        my_list.pop()
        largest(my_list)

this will give me the right value

smajlo
  • 972
  • 10
  • 21
prayoga yoga
  • 133
  • 7

0 Answers0