2 Replies - 420 Views - Last Post: 05 April 2012 - 06:23 AM Rate Topic: -----

#1 lfsmwilliamson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3039
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1688
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1