ok so i really dont understand this whole recursive stuff and i need some help on a simple program.
the problem is to write and test a recursive function max to find the largest number in a list.

here is a non recursive function that ive made,

def Max(li):
    nums = li
    for num in nums:
        if num > biggest:
            biggest = num
    return biggest

def main():
    li = eval(input("please enter a list of numbers: "))
    print("The biggest number is: ", Max(li))

can any one help me turn this into a recursive function?

7 Years
Discussion Span
Last Post by Gribouillis

You don't need recursion for this. Are you sure you are not supposed to sort in reverse order, i.e. maximum value is first. You can think of recursion as a while loop, with a function calling itself instead of looping.


heres the problem from the book im working from ( zelle"s python programing)

question 4 in chapter 13-
"write and test a recursive function max to find the largest number in a list.
the max is the the larger of the first item and the max of all other items"

maybe im interpreting the question wrong but to me it means that i have to create a function that simply puts out the largest value but i need to do it using recursion and i just dont understand how to do so


I think the book means

def Max(L):
    if len(L) <= 1:
        if not L:
            raise ValueError("Max() arg is an empty sequence")
            return L[0]
        m = Max(L[1:])
        return m if m > L[0] else L[0]

but don't use it in real code, use the the builtin max function !

Edited by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.