Hi, im a begginer to python, and i've been doing it a small while now, i've been attempting some puzzles of a website, and have met a small problem, and i can't find it. My code is below and i will explain my problem, ask how to sort it, and ask if i am missing something important.
a = range(1,1000)
b = []
d = 0
for n in a:
while n != 1:
if n % 2 == 0:
b.append(n)
n = n/2
if n % 2 != 0:
b.append(n)
n = n*3 + 1
else: b.append(n)
if len(b) > d:
d = len(b)
b = []
Now the aspects of this code is to find out how long it takes to get to 1, using the 'Collatz Problem' i take the value of n to be inside the range, and if its not 1, i want it to go through the number, decide if its even or odd, then perform a calculation, firstl, im recording the number in a list, so i know the starting number, and then it will perform the calculation, i want it to continue this loop until it reaches 1, im not sure how to do this, i have the else statement at ehd end because i believe that is what i need, i then will take the length of b which is my list, and see if it is higher than my previous list, i then want it to record if so and erase the list, so i can start over at 0, with my next number, but i seem to not be able to make my list work, and i get memory failure of B several times, if someone can help, it would really be apprechiated.