+ 1

Why the right answer of the code showed in the description is 6 (I take it from a challenge)

What is the output of this code? A = [8, 0, -4, -6, 1] for n in A: if n < 0: A.append(abs(n)) x = n print(x)

30th Apr 2019, 1:47 PM
mihai
mihai - avatar
2 Antworten
+ 5
n = 8 n < 0 False . . . n = -4 n < 0 True A becomes [8, 0, -4, -6, 1, 4] n = -6 n < 0 True A becomes [8, 0, -4, -6, 1, 4, 6] Then there are no more negative values, the last value of A Is 6, so x = 6
30th Apr 2019, 1:51 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Thanks man I got it :)
30th Apr 2019, 2:02 PM
mihai
mihai - avatar