MIQIWEI 发表于 2020-8-16 17:06:32

求助 怎么改 谢谢!

本帖最后由 MIQIWEI 于 2020-8-16 17:14 编辑

get_pair_of_largest_numbers(numbers),该函数将数字列表作为参数,并返回具有最大和的相邻数字对作为元组。您可以假定列表不为空(至少两个或更多元素),并且每个元素都是有效数字。


def get_pair_of_largest_numbers(numbers):
    result = + numbers for index in range(len(numbers)-1)]
    max_value = result
    for index in range(len(result)):
      if result > max_value:
            max_value = result
    return max_value


Test        Result
print(get_pair_of_largest_numbers())
(-1, 11)
print(get_pair_of_largest_numbers([ 10, 20, 3, 31, 8, 42]))
(8, 42)
print(get_pair_of_largest_numbers([ 10.5, -3]))
(10.5, -3)

Twilight6 发表于 2020-8-16 17:28:29

def get_pair_of_largest_numbers(numbers):
    result = + numbers for index in range(len(numbers)-1)]
    max_value = result.index(max(result))
    return tuple(numbers)

print(get_pair_of_largest_numbers())
print(get_pair_of_largest_numbers([ 10, 20, 3, 31, 8, 42]))
print(get_pair_of_largest_numbers([ 10.5, -3]))
页: [1]
查看完整版本: 求助 怎么改 谢谢!