[Python] Adding all numbers before a specific index in a list

Discussão em 'Python' iniciado por Stack, Novembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    user_list = list(input('Enter a list of numbers'))
    for i in range (len(user_list )):
    user_list = int(user_list)
    print(user_list)

    first_number = user_list [0]
    second_number = user_list [0 + 1]
    new_list = []
    for i in range (len(user_list )):
    if i == 0:
    first_number = user_list[0]
    new_list.append(i)
    else:
    sum(user_list[:i + 1])
    new_list.append(i)
    print(new_list)


    This is a code that is supposed to take a list of numbers and return the cumulative sum; that is, each element of the new list is the sum of it plus all the elements before it in the old list. However, I don't know how exactly does this code work, and I want to make it add the previous indices and add them to a new list.

    Continue reading...

Compartilhe esta Página