I am a beginner in Python. Now I am trying to create an algorithm that will build a Pascal's triangle.
My first step is to create a basic iteration that will use a list called current level and create the next level list based on it as follows:
- Add 0 to the first number to create the left number and insert it into a list.
- Create the inner row of numbers and insert them into a list
- Add 0 to the last number to create the right number and insert it into a list.
My code includes only one iteration, but iteration is not a problem now. I do not understand how to address the elements of the list by their index correctly.
Can somebody give me a hint?
Thanks!
currentlevel = [1]
nextlevel = []
leftnumber = 0 + currentlevel[0]
nextlevel.append(leftnumber)
for item[index] in currentlevel:
if index > 1 and index < len(currentlevel)-1:
item[index] = item[index] + item[index+1]
nextlevel.append(item[index])
else:
rightnumber = currentlevel[-1] + 0
nextlevel.append(rightnumber)
currentlevel = nextlevel
print(currentlevel)