4

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
Stephen
  • 43
  • 1
  • 1
  • 4
  • "Anything I've tried so far hasn't worked" - Could you give some examples of things you've tried? That always makes it easier for us to guide you. – DGH Aug 22 '12 at 22:56
  • This is like going to a home decoration site and saying "I'm trying to paint my house but anything I've tried hasn't worked" You need to get better at asking questions if you want to get useful help. Tell us __what hasn't worked__? – John La Rooy Aug 22 '12 at 23:22

4 Answers4

7

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?

Levon
  • 138,105
  • 33
  • 200
  • 191
  • that code is already given. The entire task is to just add one line! – John La Rooy Aug 22 '12 at 23:08
  • @gnibbler I know .. like I said, he's quite close and has done of the bulk of the work. Do you think it would be helpful to explicitly say he's just missing one statement? – Levon Aug 22 '12 at 23:09
  • I think you misunderstand. The question on codeacademy already gives those 5 lines as part of the question – John La Rooy Aug 22 '12 at 23:12
  • @gnibbler Oh, yes I *totally* misunderstood what you were saying. I see now .. thanks for clarifying. – Levon Aug 22 '12 at 23:13
  • Got it! Thanks to you and gnibbler :D. I'll edit my question to show what I did. – Stephen Aug 23 '12 at 11:48
  • @Levon Okay I clicked the check mark on your answer but I'm not allowed to "upvote" unless I have at least 15 reputation. Thanks again! – Stephen Aug 23 '12 at 11:58
2

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?

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • I think that writing "create a variable num and *assign* the value 1 to it" might be clearer to someone new to programming in place of using "bind". – Levon Aug 23 '12 at 00:43
  • +1 for stepping through the code and getting OP to try to answer the right question at the end. – Levon Aug 23 '12 at 00:44
-1

For loops! Meh I say!

n=10
sum(range(n+1))
Aesthete
  • 18,622
  • 6
  • 36
  • 45
-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?

  • 1
    This seems to be more of a question than an answer. You should ask it separately, perhaps linking to this one for context. (Fewer people see it when it's here.) As for your question, it's pretty simple. The return statement does not facilitate the loop. You could take it out, and the loop would be fine. What it does is supply the value to whatever called the function. Your print will work without it, but it is needed if you expect to do something like `result = problem1_3(14)`. Without the return, the result would be None. You might be interested in http://stackoverflow.com/q/7129285/5827958 – zondo Apr 29 '17 at 10:44