递归函数中的计数器-我有一个函数试图在列表中找到最大的数字。当然,j将被设置回0,计数器声明应该在哪里?
import random
list = []
for i in range(0, 10):
number = random.randint(0, 100)
list.append(number)
print(list)
def bigger_number (list):
j = 0. #where to place this
n = list[0] #where to place this
while j < 9:
if n >= list[j]:
list[j] = n
j+=1
bigger_number(list)
print("The biggest number in the list is : ",n)
bigger_number(list)
答案 0 :(得分:1)
根据我的理解,您希望使用递归解决方案从数组中获取更大的数量。
如果是,请尝试以下功能:
# ListA = [123,45,89,58]
# lenA = len(ListA)
def bigger_Number(ListA, lenA):
if (lenA == 1):
return ListA[0]
return max(ListA[lenA - 1], bigger_Number(ListA, lenA - 1))