Python: Recursive function to find the largest number in the list

前端 未结 10 648
长情又很酷
长情又很酷 2020-12-10 21:04

I\'m trying to do a lab work from the textbook Zelle Python Programming

The question asked me to \"write and test a recursive function max() to find the

10条回答
  •  再見小時候
    2020-12-10 21:30

    Your understanding of how recursion works seems fine.

    Your if-block is messed up, you have two elses to one if and the alignment is out. You need to remove your first else and un-indent everything below the if one level. eg:

    def Max(list):
        if len(list) == 1:
            return list[0]
        else:
            m = Max(list[1:])
            return m if m > list[0] else list[0]
    
    def main():
        list = eval(raw_input(" please enter a list of numbers: "))
        print("the largest number is: ", Max(list))
    
    main()
    

提交回复
热议问题