trouble with a simple for loop

Page 1 of 1

2 Replies - 421 Views - Last Post: 05 April 2012 - 06:23 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=273936&amp;s=56a2d5d35ed028366a2a0290c9cb23a3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 lfsmwilliamson

Reputation: 0
• Posts: 2
• Joined: 04-April 12

trouble with a simple for loop

Posted 04 April 2012 - 11:15 PM

trying to write code that gives me the square of a range.
so far i have

```for i in range(h):
g = h*h

```

the error i'm getting is an incorrect result at 0 through turingscraft

exact error code is

0 null g does not contain the correct value
Is This A Good Question/Topic? 0

Replies To: trouble with a simple for loop

#2 Atli

• D.I.C Lover

Reputation: 3040
• Posts: 4,554
• Joined: 08-June 10

Re: trouble with a simple for loop

Posted 05 April 2012 - 12:37 AM

I don't know how turingscraft works, but there doesn't appear to be anything wrong with that loop. The only thing that would cause it to fail is if h isn't set properly.

#3 sepp2k

• D.I.C Lover

Reputation: 1690
• Posts: 2,553
• Joined: 21-June 11

Re: trouble with a simple for loop

Posted 05 April 2012 - 06:23 AM

Your loop sets g to h*h h times. That's exactly the same as setting g to h*h one time, so your loop is superfluous.

When you the say "the square of a range", do you mean the sum of the squares of each number in the range? If so you should square the numbers in the range, not h. And you should sum them instead of overwriting g at each iteration of the loop.