x = 3
y = x +y keep doing the iteration if x >0 if x = x - 1
that is, you subtract 1 from x for every iteration until you get to 0
I don't know why I'm not getting this. The answer is 9 by the way.
The attempt at a solution is shown below.
I add 0 and 3,
subtract 1. x is now 2.
then I add 2 and 3. subtract 1 from x. x is now 1
then I add 1 and 5. subtract 1 from x. x is now 0
Now finished with the while statement we go and print y
Basically I am adding x and y to get the new value of y and then repeating
Please explain what I'm doing wrong and how I should be going about it.
更新1:
the answer is 9 because in python, the programming language that i ran this on said that it was. the actual code, if you're interested is y = 0 x = 3 itersLeft = x while (itersLeft 0): y = y x itersLeft = itersLeft - 1 print y
更新2:
there should be a minus sign in between the y and x on line 5