Stephen
Stephen

Reputation: 43

Python:How to make the sum of the values of a while loop store into a variable?

I'm just a beginner :P. I'm doing a tutorial about while loops on Codeacademy "Click here!" , but I've gotten stuck on this part: Write a while loop which stores into "theSum" the sum of the first 10 positive integers (including 10).This is what it gives you to work with:

theSum = 0
num = 1
while num <= 10:
    print num
    num = num + 1

It prints out the numbers 1 to 10 on seperate lines in the console. Can anyone explain to me how I can get it to store the sum of the values in the variable "mySum"? Anything I've tried so far hasn't worked for me. :(

EDIT: Okay so I just tried this:

theSum = 0
num = 1
while num <= 10:
    num += 1
    mySum = num
    mySum = mySum + num

print mySum

This gives me 22, why is that? Am I in anyway close? (Thanks for all the replies but I'll try again tomorrow.)

EDIT: Okay, I got it! Thank you for the help. :)

mySum = 0 
num = 1 
while num <= 10: 
    mySum += num 
    num += 1    
print mySum

Upvotes: 4

Views: 83332

Answers (4)

Conor McGrath
Conor McGrath

Reputation: 1

I also greatly struggled with this.

This was my solution but I got help from interactivepython.org (http://interactivepython.org/runestone/static/pip2/IndefiniteIteration/ThewhileStatement.html)

I couldn't figure out how to do this without the 'return' function. See solution and explanation below:

def problem1_3(x):
    my_sum=0
    count = 1
    while count<=x:
        my_sum=my_sum+count
        count = count+1
    return my_sum
    print(my_sum)

Lets assume you set x = 3 The way I believe python interprets this is as follows: set my_sum = 0 and count = 1 1. First iteration with the while loop: 1 <= 3 : True so my_sum = 0+1 plus count increases by 1 and now count = 2 The 'Return my_sum' is key because it allows my_sum to circle back to the top of the loop as 1 now instead of 0.

  1. Second iteration of the while loop: 2 <= 3 : True so my_sum = 1+2 ; count again increases by 1 so now count = 3 Return my_sum again returns the new value of 3 for my_sum to the top of the loop

  2. Third iteration of the while loop: 3 <= 3 : True so my_sum = 3+3 ; count increases by 1 so now count = 4 Return my_sum again returns the new value of 6 for my_sum to the top of the loop

  3. Fourth iteration of while loop never happens since 4 <=3 : False Now the program prints my_sum which now is equal to 6.

However, I'd think there would be an easier way to do this. Is there a way for python to generate a list and then sum the values in the list? For example, could I write a program called sumlist(n) where python listed the integers from 0 to n and then added them up?

Upvotes: -1

John La Rooy
John La Rooy

Reputation: 304175

Lets take a dry run through the code you've posted. I've numbered the lines so I can refer to them.

1. num = 1
2. while num <= 10:
3.     num += 1
4.     mySum = num
5.     mySum = mySum + num

6. print mySum

Here's a dry run

1. simple enough, create a new variable `num` and bind it to the number `1`
2. `num` is less than 10, so do the body of the loop
3. `num` is `1` so now bind it to `2`
4. create a new variable `mySum` and bind to `2` (same as num)
5. `mySum` is `2` and `num` is `2` so bind `mySum` to `4`
Back to the top of the loop
2. `num` is less than 10, so do the body of the loop
3. `num` is `2` so now bind it to `3`
4. bind `mySum` to `3` (same as num)
5. `mySum` is `3` and `num` is `3` so bind `mySum` to `6`
Back to the top of the loop
2. `num` is less than 10, so do the body of the loop
3. `num` is `3` so now bind it to `4`
4. bind `mySum` to `4` (same as num)
5. `mySum` is `4` and `num` is `4` so bind `mySum` to `8`
Back to the top of the loop
...

looks like something is not going right. Why are you doing this mySum = num inside the loop? What do you expect it to do?

Upvotes: 2

Aesthete
Aesthete

Reputation: 18848

For loops! Meh I say!

n=10
sum(range(n+1))

Upvotes: -1

Levon
Levon

Reputation: 143022

The code you have already shows almost everything needed.

The remaining problem is that while you are correctly generating the values to be added (num) inside your while-loop, you are not accumulating these values in your variable theSum.

I won't give you the missing code on purpose, so that you can learn something from your question ... but you need to add the value of num to your variable theSum inside the loop. The code for doing this (it's really only one statement, i.e., one line of code) will be somewhat similar to how you are dealing with/updating the value of num inside your loop.

Does that help?

Upvotes: 7

Related Questions