473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

debugging a while loop

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
2 14607
bartonc
6,596 Expert 4TB
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
2,851 Expert Mod 2GB
Barton - It's nice to see you again. We miss you.

-BV :)
Feb 24 '09 #3

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

Similar topics

9
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
7
by: DaVinci | last post by:
I am writing a pong game.but met some problem. the ball function to control the scrolling ball, void ball(int starty,int startx) { int di ,i; int dj,j; di = 1; dj = 1; i = starty;
1
by: pauljturner99 | last post by:
Hi, I'm trying to pass a parameter from a for loop to the nested while loop but only the first counter is passed. Here is the code: dim ctr redim ctr(5) ctr(0) = 2 ctr(1) = 4 ctr(2) = 6
3
by: libsfan01 | last post by:
hi all in my js code i have a while loop contained within a while loop executed on the basis of a conditional if statement, what i want to do is end the entire function on the last execution on...
14
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
6
by: mgcclx | last post by:
For loop and while loop. which one is faster? I see many articles fighting over it and different people come up with different results.
5
by: =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?= | last post by:
Good evening all, I am trying to write a process that uses a while loop to cycle multiple files from an array throught the StreamReader Process. The whole thing works using: Dim...
3
by: numlock00 | last post by:
I have a nested 'while' loop that won't repeat, no matter how many times the outer loop repeats. The outer loop reads through an array of elements; the inner loop Ithe 'while' loop) is supposed to...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.