0
Is there a way to do this efficiently
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
9 Respostas
+ 3
Ervan sorry, I can't take your post serious.
I laugh with you.
+ 1
s = sum(map(int, input().split()))
print(s)
This is a SHORTER WAY
+ 1
Ervan , it can be done shorter by using built-in sum function. Look at the example.
https://code.sololearn.com/czceW839hkxG/?ref=app
+ 1
With "for i in list" the "i" already is the element of the list – just add it to sum – no need to check it with "if"
+ 1
Delicate Cat These codes perform the same function, but it is preferable that you do not use sum as a variable. Because python uses it as a function
+ 1
l=[1,2,3,4,5,4,2,5]
print(sum(l))
this is simplest way
0
#btw this is how i do it
list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sum = 0
for i in list:
if i == 1:
sum+=1
elif i == 2:
sum+=2
elif i == 3:
sum+=3
elif i == 4:
sum+=4
elif i == 5:
sum+=5
elif i == 6:
sum+=6
elif i == 7:
sum+=7
elif i == 8:
sum+=8
elif i == 9:
sum+=9
print(sum)
0
Surely there is a SHORTER WAY TO DO IT than that
0
Open the code below to view how I solved your problem all the explanation is within the code each statement that requires explanation I add comment before it.
https://code.sololearn.com/c0YL9jKd8Cxm/?ref=app