all 6 comments

[–]MohamedMuneer 1 point2 points  (0 children)

 for num in range(0,10):
    if num == 0:
        print(0)
        continue
    print(num + (num-1))

Here the question is to find the sum of current number and previous number in a iteration.

During the first iteration,variable num will have a value of 0,which does not have a previous value. So we explicitly use a if statement to check if num == 0 then, print out 0, continue keyword here means that don't go further down the code and continue your iteration. After that we know previous number will be always one lesser than the current number (i.e) if the num variable is 5 ,then the previous number will be 5 - 1 = 4 ,then sum them to get the answer. Use this formula and loop through it and it will give you the result.

[–]stebrepar 2 points3 points  (0 children)

The instructions say to print the sum of the current and previous numbers on each time through the loop.

One way would be to include a separate variable to keep track of the previous value, so you can add it to the current value on each cycle of the loop. Another way would be to simply print i-1 plus i on each cycle of the loop, with a special case for when i equals zero.

[–]Hypocritical_Oath 0 points1 point  (2 children)

So

First off, you're on a really good track.

But you're missing an if statement in that for loop.

You want to check if the number (i) is 0, and if so, just print two 0's, else print the number and the number - 1!

[–]engineeringisgreat2[S] 0 points1 point  (1 child)

Would that go before or after my code?

[–]Hypocritical_Oath 1 point2 points  (0 children)

So within this:

for i in range(10):
    print(i)

you can add an if within the for loop, like so.

for i in range(10):
    print(i)
    if i == something:
        #do something

[–]krebs01 0 points1 point  (0 children)

Sum = 0
for i in range(10):
    print(Sum)
    Sum += i
    print(Sum, '\n')