By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,551 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,551 IT Pros & Developers. It's quick & easy.

debugging a while loop

P: 31
Expand|Select|Wrap|Line Numbers
  1. k += 1
  2. total=0
  3. while k <= 50:
  4.     total += k * k
the questions is below. when i input in this code and run it, it says that it is an infinite loop, i dont know what else to change.can someone help?




Use the variables k and total to write a while loop that computes the sum of the squares of the first 50 counting numbers, and associates that value with total . Thus your code should associate 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 with total . Use no variables other than k and total .
Feb 23 '09 #1
Share this Question
Share on Google+
2 Replies


bartonc
Expert 5K+
P: 6,596
Although this is homework and there are rules against that, you are so close that it would be a shame to tell you that you can't get help here... Just remember to increment whatever variable you are testing in the loop!

Expand|Select|Wrap|Line Numbers
  1. k = 1
  2. total=0
  3. while k <= 50:
  4.     total += k * k
  5.     k += 1
Some may prefer this style
Expand|Select|Wrap|Line Numbers
  1. k = 0
  2. total=0
  3. while k <= 50:
  4.     k += 1
  5.     total += k * k
because the loop test and the increment stay close together.
Feb 23 '09 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Barton - It's nice to see you again. We miss you.

-BV :)
Feb 24 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.