0
Python Core 27.2 "Iteration"
I'm not sure what to do here: for loops allow you to easily iterate through lists. Given a list of numbers, output their sum. Sample Input 1 3 7 5 Sample Output 16 Output the sum after the loop. list = [1, 2, 3, 4, 5, 6, 7, 8, 9] sum = 0 #your code goes here
3 Respostas
+ 8
cory p
to solve this task, you need the following steps: (there a various ways to do this task, this is one of them):
1 - as the input values has to be given in one line, the input has to be spit to individual elements to a list
2 - create a variable that can hold the running sum of the values in the list
3 - use a loop to iterate through the list (numbers are still strings)
4 - in each loop iteration you will get one value, that has to be converted to int and added to the sum variable
5 - after last iteration loop, the variable holds the sum of all numbers
6 - print the result of the variable
happy coding and good success!
+ 2
You have to give the sum of the every value inside the list.
For this, according to the question you have to use a loop.You can iterate each value of the list using loop like that.
Like that,
for i in list:
...
Now try it by yourself.
0
list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sum = 0
#your code goes here
for i in list:
if int(i):
sum += 1
print(sum)
My output: 9