0

Simple for loop example

Hi this is a simple for loop example I have no idea how to approach. I only know while, len functions (not count or sum in python yet) how can I solve? When This is code I'm given: #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 list = [1, 2, 3, 4, 5, 6, 7, 8, 9] sum = 0 #your code goes here for x in list: if(x == 1): sum += 1 print(sum)

12th Dec 2020, 4:51 AM
Fast
18 Answers
+ 4
x = [42, 8, 7, 1, 0, 124, 8897, 555, 3, 67, 99] sum = 0 for x in x: sum += x print(sum) ##hehe
1st Feb 2022, 9:56 AM
Sandipan Karmakar
+ 2
Thank you Jan I will study that
12th Dec 2020, 5:34 AM
Fast
+ 2
this is 100% correct list = [1, 2, 3, 4, 5, 6, 7, 8, 9] sum = 0 #your code goes here for i in list: sum+=i print(sum)
24th May 2022, 1:34 PM
Hrithika Reddy
Hrithika Reddy - avatar
+ 1
Fast Why are you checking x == 1 when you just need to add values of list? Just do sum += x
12th Dec 2020, 4:58 AM
A͢J
A͢J - avatar
+ 1
So far my code is: sum = 0 for x in list: sum += x print(sum) Is this what is happening? Sum = 0+1 Sum = 1+2 Sum = 3+3 Sum = 6+4 Sum = 10+5 Sum = 15+6 Sum = 21+7 Sum = 28+8 Sum = 36+9 Sum = 45
12th Dec 2020, 5:37 AM
Fast
+ 1
### x = [42, 8, 7, 1, 0, 124, 8897, 555, 3, 67, 99] sum = 0 for i in x: sum=sum + i print(sum) Perfect answer 😊
23rd Feb 2022, 9:19 AM
MANOJ BABU MALLAKUNTA
+ 1
x = [42, 8, 7, 1, 0, 124, 8897, 555, 3, 67, 99] sum = 0 for x in x: sum += x print(sum)
5th May 2022, 1:12 PM
PRAKHAR PRITHVIRAJ GOND
PRAKHAR PRITHVIRAJ GOND - avatar
0
This is a code playground beginner challenge i don't know what print(f'sum is {s}') yet
12th Dec 2020, 4:59 AM
Fast
0
Fast Yes Here we are adding next value with previous total value.
12th Dec 2020, 5:41 AM
A͢J
A͢J - avatar
0
Try list = [1, 2, 3, 4, 5, 6, 7, 8, 9] sum = 0 #your code goes here for x in list: if(x == 1): sum += 45 print(sum)
8th Oct 2021, 4:42 PM
Raoof Zakarna
0
x = [42, 8, 7, 1, 0, 124, 8897, 555, 3, 67, 99] r = 0 for n in x : r+=n print(r)
28th Apr 2022, 12:08 PM
Dharmendra Pandit
Dharmendra Pandit - avatar
0
x = [42, 8, 7, 1, 0, 124, 8897, 555, 3, 67, 99] count = 0 for x in x: count += x print(sum)
20th May 2022, 2:11 PM
Evgen Shamanayev
Evgen Shamanayev - avatar
0
list = [1, 2, 3, 4, 5, 6, 7, 8, 9] #0 is the first member sum = 0 #do for all members in list for l in list: #next sumation is equal sum of last sum plus next one #sum of member = member0 + next member sum=sum+l #you may use sum+=1 as well print(sum) #your code goes here
9th Jul 2022, 6:52 AM
Hamed Taherzadeh
Hamed Taherzadeh - avatar
0
list = [1, 2, 3, 4, 5, 6, 7, 8, 9] sum = 0 #your code goes here for i in list: sum+=i print(sum)
15th Nov 2022, 8:00 AM
Pooja Patel
Pooja Patel - avatar
0
x = [42, 8, 7, 1, 0, 124, 8897, 555, 3, 67, 99] sum = 0 for x in x: sum += x print(sum)
19th Mar 2023, 4:23 PM
المهندس أحمد
المهندس أحمد - avatar
0
list = [1, 2, 3, 4, 5, 6, 7, 8, 9] sum = 0 #Sample Input 1 3 7 5 for x in list: if x%2 == 1: sum += x elif x == 8: break print(sum) #Sample Output 16
16th Apr 2023, 2:03 AM
AMINE MOUTAOUAKKIL
- 1
x = [42, 8, 7, 1, 0, 124, 8897, 555, 3, 67, 99] sum = 0 for x in x: sum += x print(sum) done...!
5th Mar 2022, 5:39 AM
Nikhila
- 6
using sum() function: list = [1, 2, 3, 4, 5, 6, 7, 8, 9] total = sum(list) print(total)
21st May 2021, 6:39 AM
Mohammed Ebrahim
Mohammed Ebrahim - avatar