0

Help me please with “for” in Python

Name list of numbers print their sum. The last line, when displayed, shows the amount. How to display only the last row and not all? list = [1, 2, 3, 4, 5, 6, 7, 8, 9] sum = 0 for n in list: sum = n + sum print(sum)

14th Mar 2021, 8:48 AM
Рамин Барциц
Рамин Барциц - avatar
6 Answers
+ 3
remove the indentation before the print statement :)
14th Mar 2021, 8:53 AM
Alexander Crum
Alexander Crum - avatar
+ 2
your print is inside for loop. And You can also use this a = [1,3,5,90] print(sum(a))
14th Mar 2021, 9:21 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 1
the "print(sum)" line is indented one block too much. unindent it so its on the same level as the start of the for loop and it will only print the final sum. ** you have the call to print INSIDE the for loop, all you gotta do is take it out
14th Mar 2021, 8:54 AM
Slick
Slick - avatar
+ 1
Alexander, thank you, its working!
14th Mar 2021, 8:55 AM
Рамин Барциц
Рамин Барциц - avatar
0
Hi! Please, show us your efforts for better help
14th Mar 2021, 8:50 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
3 6 10 15 21 28 36 45
14th Mar 2021, 8:53 AM
Рамин Барциц
Рамин Барциц - avatar