+ 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)
7 odpowiedzi
+ 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)
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.
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)
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)
0
ans is
4
0
the ans is 4
0
ans is 4