So I have been working on Pascal's triangle and going about it a different way then most. I am attempting do to a series of if and elif statements to check for the values in rows and columns of those rows. However my novice skills in python are stopping me from achieving my goal. Here is what I have so far:

h=input("Please enter the height: ")

mytri=[ ]

for row in range(0,h+1):
newrow=[ ]
if row==0:
    newrow=[1]
elif row==1:
    newrow=[1,1]
else:
    for i in range(row):
        if i==row[0]:
            newrow.append(1)
        elif i==row[-1]:
            newrow.append(1)
        else:
            a=(row-i)*(i-1)
            b=row-1
            c=a+b
            newrow.append(c)



mytri.append(newrow)
print mytri

As you can probably tell from the code I do not quite know where my next step is. Like I said though my problem is trying to locate where a number is in a certain row and column. The only outcome I can seem to get is 1,10. If anyone has any insight into this madness I'll be ears open.

Thanks.

Recommended Answers

All 4 Replies

Your indentation is incorrect so it is difficult to tell what you are trying to do.

"i" will never equal -1 (and you should not use "i", "l" or "O" as they can look like numbers)

elif i==-1:

And this statement does not make sense because you seem to be calling two functions and assigning the return value to "a".

a=(row-i)(i-1)

You might want to look through something like Thinking in Python to get you started. Hopefully this will help you.

h=input("Please enter the height: ")
 
for row in range(0,h):
    new_row=['1' for ctr in range(row)]
    new_row.append("1")       ## "1" extra required
    print new_row

Yes I do understand it is confusing.. I am having some trouble understanding the psuedocode. Its ask for after line 12 else,
for each column in the row
if column of row is 0,
do x
elif the column is at the end of the row,
do x

the last part is trying to find the values of a number 1 row back and 1 column back from the current spot. I just have no idea how to search for spots in a column.

The first column of a list is row[0].
The last column of a list is row[-1] or row[len(row)-1] = same thing

Your indentation is incorrect so it is difficult to tell what you are trying to do.

"i" will never equal -1 (and you should not use "i", "l" or "O" as they can look like numbers)

elif i==-1:

And this statement does not make sense because you seem to be calling two functions and assigning the return value to "a".

a=(row-i)(i-1)

You might want to look through something like Thinking in Python to get you started. Hopefully this will help you.

h=input("Please enter the height: ")
 
for row in range(0,h):
    new_row=['1' for ctr in range(row)]
    new_row.append("1")       ## "1" extra required
    print new_row

that def works, the problem in my code i believe starts at 13

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.