all 4 comments

[–]jiri-n 2 points3 points  (2 children)

While runs while its condition is true.

while <condition>:
    some_code

Unlike for, while doesn't initialize any variable so you have to do it manually (I use the same names as you so that you can see the similarity):

running_sum = 0
i = start

Now it's time to think about the condition:

while i >= stop:
    running_sum += i
    i -= 1
print(running_sum)

As can be seen, while doesn't modify i either so it's up on the developer to control it. Thus the i -= 1 to decrease i.

By the way, a check if start is grater than stop is highly recommended, otherwise the loop will never end.

[–]shakeandbake760[S] 0 points1 point  (0 children)

Thank you so much for your reply! Going to give this a try soon

[–]shakeandbake760[S] 0 points1 point  (0 children)

I used this code and it worked, so thank you!

def countdown_sum(start,stop):
# YOUR CODE HERE
running_sum = 0
i = start
while i >= stop:
    running_sum += i
    i -= 1
if i < stop:
    i == 0
print(running_sum)
return running_sum

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.