Python Forum
Issues with Inserting Values into an Empty List with a While Loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issues with Inserting Values into an Empty List with a While Loop
#1
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!
Reply
#2
Capital C (bad name as it doesn't say what it is), never changes under the while, so it never reached the maximum.
Reply
#3
Ok, I see now and got it to work. Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can anyone spot why this function is returning an empty list? hhydration 2 1,816 Nov-18-2020, 06:16 AM
Last Post: deanhystad
  create empty sets for keys in a dictionary and add values to the set naughtysensei 1 2,477 Nov-03-2020, 08:32 AM
Last Post: DeaD_EyE
  Why does this function print empty list? hhydration 1 1,501 Oct-28-2020, 02:03 AM
Last Post: deanhystad
  Python keeps inserting NULL values into table card51shor 18 4,581 Jun-09-2020, 07:05 AM
Last Post: buran
  How can I run a function inside a loop every 24 values of the loop iteration range? mcva 1 2,101 Sep-18-2019, 04:50 PM
Last Post: buran
  Issues with while loop, while (var != 0): does not work, but while (var !=''): works danilo 2 2,024 Apr-15-2019, 12:08 AM
Last Post: danilo
  dictionaries and list as values Pippi 6 3,423 Apr-13-2019, 09:05 AM
Last Post: perfringo
  Turtle Graphics Card Values in a List Muzz 0 2,326 Apr-11-2019, 12:55 PM
Last Post: Muzz
  2D arrays and appending values using a loop Pythonhelp82 4 5,415 Mar-25-2019, 03:23 AM
Last Post: Pythonhelp82
  why is this not filling my empty list? Siylo 4 3,098 Jan-21-2019, 05:27 PM
Last Post: Siylo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020