Python Forum
Issues with Inserting Values into an Empty List with a While Loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Issues with Inserting Values into an Empty List with a While Loop (/thread-12774.html)



Issues with Inserting Values into an Empty List with a While Loop - TommyMer - Sep-11-2018

Hi, I am new here and this is my first post,

I am working on a homework assignment in which I am supposed to print the Catalan Numbers up to 1 billion (1e9).

"The Catalan numbers C_n are a sequence of integers 1, 1, 2, 5, 14, 42, 132... that play an important role in quantum mechanics and the theory of disordered systems. (They were central to Eugene Wigner's proof of the so-called semicircle law.) They are given by

C_o = 1, C_n+1 = ((4*n + 2)/ (n + 2)) * C_n"

I am using Jupyter Notebook to process Python.

For the first part of this assignment, I am given an empty list in the first cell that I need to fill with the Catalan Numbers and a for loop that should run through the list when it is filled and prints the list. I have to do this method since I can't change the initial code (the name of the list in the first cell and the whole second cell).

I was able to print the first 3 numbers, but only by typing individual code for each which is not a good method.
When I try to use a while loop and the insert() function to add to the empty list, I end up getting an infinite loop that crashes my computer; it doesn't even print the first element of the list. My professor says it seems like the assignment that I use to define a given element isn't being modified which i don't understand why since I defined it with the Catalan Function and that it works until I add the while loop.

I want to know why this isn't working because it seems so simple, and what needs to be done to fix this, please.

Here is screenshot of what I have:
[Image: Catalan_Numbers_Python.jpg]


If the link doesn't work here is the code for cell 1:

catalan_1e9 = [] # I can't edit this line; it is given
# YOUR CODE HERE


catalan_1e9.insert(0, 1) # Insert the first term into the list
n = 1 # Set n as an iterator representing the index of the list; start at index 1
C = int(((4*n+2)/(n+2)) * catalan_1e9[n-1]) # Define C by the Catalan function and force output to be integers

while C <= 1e9:

catalan_1e9.insert(n, C) # Use the While loop to insert the Catalan numbers into the list until 1e9 is reached

n+=1 # Increase the index by one for the next iteration

return catalan_1e9


And this is what is given to me in cell 2; I can't modify this code in any way:

# do your results look reasonable?
for c in catalan_1e9:
print©

There are no syntax errors, but it causes an infinite loop that eventually crashes my computer within a few minutes. Everything gets screwy when I add the while loop block and I am not sure why.

If something isn't clear, feel free to ask and I will try to clarify.

Any feedback is appreciated, and Thanks in advance!


RE: Issues with Inserting Values into an Empty List with a While Loop - woooee - Sep-12-2018

Capital C (bad name as it doesn't say what it is), never changes under the while, so it never reached the maximum.


RE: Issues with Inserting Values into an Empty List with a While Loop - TommyMer - Sep-12-2018

Ok, I see now and got it to work. Thank you!