Weird while loop exercise


Does anyone know how to do this while loop exercise? I have the solution to it, but I want to see if someone can solve it without it. I would like to know how you came to that conclusion if possible

Ignore the console output, that’s from a previous exercise i did earlier.

I mean, okay.

counter = 0
total = 0

while counter < 100:
    counter += 1
    total += counter

print(total)
1 Like

It’s not really a while loop but:

print(sum(range(1, 101)))
1 Like

The output value is 5050

How did you come to your conclusion? How did you know we had to add total to counter and create a counter that increases by 1 in each cycle

Yes, that is the sum of all numbers from 1 to 100

1 Like

um
I used my
:brain:
:sunglasses:
but it was kinda obvious, you already created the counter for me and in a while loop like this you have to increment it or the loop goes forever

2 Likes

Not a while loop but I started with one and then got to this.

num = int(input("number?"))
i = float((num/2 + .5) * num)
print(i)

First I did this.

num = 100
i = num + 1
total = 0
while i > 0:
    i -= 1
    print(i)
    total +=i
print(total)
print(total/num)

then I thought hmm so for 100 if i divide the total by 100 it gives me 50.5 , that’s close to half, so, what if i just divide 100, by 2 and then add .5, and then multiply that by 100.

((num / 2 + .5 ) * num)

This just results in ii = num, right?

Yeah i was using it for the same thing as num, before I wrote num, but forgot to delete it after. Thank you for pointing that out though.

1 Like

This was how I was going to do it lol