LU02.L01 - Summe einer Zahlenliste

def sum_of_numbers(numbers):
    return sum(numbers)
 
if __name__ == '__main__':
    numbers = [1, 2, 3, 4, 5]
    print('The sum of the numbers is:', sum_of_numbers(numbers))

oder

def sum_of_numbers(numbers):
    """
    Recursively calculates the sum of a list of numbers.
 
    :param numbers: A list of integers.
    :type numbers: list
    :return: The sum of the numbers in the list.
    :rtype: int
 
    The function operates recursively, summing the first element of the list 
    with the sum of the remaining elements. If the list is empty, the function 
    returns 0 as the base case.
    """
    if not numbers:
        return 0
    return numbers[0] + sum_of_numbers(numbers[1:])
 
if __name__ == '__main__':
    numbers_list = [1, 2, 3, 4, 5]
    result = sum_of_numbers(numbers_list)
    print(f'The sum of the numbers is: {result}')    

© Kevin Maurizi

  • modul/m323/learningunits/lu02/loesungen/pure1.txt
  • Zuletzt geändert: 2024/08/28 13:31
  • von kmaurizi