list index out of range in simple Python script

前端 未结 6 1753
余生分开走
余生分开走 2021-01-24 12:53

I just started learning Python and want to create a simple script that will read integer numbers from user input and print their sum.

The code that I wrote is

         


        
6条回答
  •  情书的邮戳
    2021-01-24 13:10

    for i in list gives the values of the list, not the index.

    inflow = list(map(int, input().split(" ")))
    result = 1
    for i in inflow:
        result += i
    print(result)
    

    If you wanted the index and not the value:

    inflow = list(map(int, input().split(" ")))
    result = 1
    for i in range(len(inflow)):
        result += inflow[i]
    print(result)
    

    And finally, if you wanted both:

    for index, value in enumerate(inflow):
    

提交回复
热议问题