+ 4

Please someone can explain me this?

What is the output of this code? nums = [1, 2, 3, 4] res=0 for x in nums: if(x%2==0): continue else: res += X print(res)

1st Nov 2021, 10:25 PM
Giuseppe Leonardi
7 Respostas
+ 8
The output is 4. The code sums X (the current number in the array) to res only when x is an odd number (because of %2)
1st Nov 2021, 10:35 PM
Guillem Padilla
Guillem Padilla - avatar
0
Just to add on top of Guillem, the current code lacks proper indentation and keep in mind that x and X(capital x) are not the same and will cause errors. so the code you wrote will not produce any output at the current state.
2nd Nov 2021, 12:04 AM
Ben Eshchar
Ben Eshchar - avatar
0
Giuseppe Leonardi continue keyword is used to skip current iteration and start execution from new iteration. So here if number is even then that will not be add in x. Btw we don't need to write else if we use continue. We can just do like this: for x in nums: if(x % 2 == 0): continue res += x print(res)
2nd Nov 2021, 3:57 AM
A͢J
A͢J - avatar
0
What is the output of this code? nums = [1, 2, 3, 4] res = 0 for x in nums: if(x%2==0): continue else: res += x print(res)
2nd Jan 2023, 1:39 PM
GORLE SAI SREE VARSHINI
GORLE SAI SREE VARSHINI - avatar
0
ans is 4
14th Jan 2023, 4:37 PM
Engr.Ayesha Liaqat
Engr.Ayesha Liaqat - avatar
0
the ans is 4
6th Mar 2023, 4:41 PM
Syed Meer Abdullah
Syed Meer Abdullah - avatar
0
ans is 4
16th Apr 2023, 11:24 AM
Serkalem Zewdu
Serkalem Zewdu - avatar