#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Infinite loop and reference error


    Anyone have any hints or tips on how to fix these problems?

    The steps I have taken have been, initializing num to prevent the num referenced before assignment issue.

    I have attempted to initialize i but that's probably not even necessary, but I'm getting an infinite loop either way.

    I tried creating a bool statement before the for loop to control the iteration, but that was pointless.

    I've tried indenting the print following the for loop and it gave me the style of the output I wanted, but not the right output.


    I have no clue what I'm doing and am so ready for this class to be over. This whole degree seems like it is not my cup of tea.

    The code is:



    def main():
    print("Non-Gauss Method")

    for i in range(1,2012+1):
    num=num+i
    print(i,i)

    print(" ",i+1)
    print("")
    print("Non-Gauss Method")
    print(num)
    den = num + i +1
    print("---------")
    print(den)
    print("")
    print("Ans = ",num/den)
    print("")
    print("")
    print("Gauss Method")
    num = 2012*2013/2
    den = 2013*2014/2
    print(num)
    print("---------")
    print(den)
    print("")
    print("Ans = ",num/den)

    main()



    I'm missing two lines, but have no clue what they are outside of initializing variables. But I don't know. My brain doesn't function like this and not having class in two weeks doesn't help. I just want to get the half of the final that I can and get what I can get.
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,575
    Rep Power
    1906
    Please use code tags around you code [code ] [ /code] (without spaces); this will keep the indention and make it easier to read.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Next time please state the problem you're trying to solve.

    By inspection, you're trying to add the integers 1 through 2012.

    printing i,i is not interesting.
    print(i, num) # might be interesting.


    # Quick implementation of the sum
    print(sum(range(2013)))



    Finally, observe that

    1+2012 gives 2013
    2+2011 gives 2013
    etceteras
    and that there are n/2 of these where n is 2012.

    By formula (n/2)*(n+1) which is always an integer since one of n or n+1 is even.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo