0

So what I am trying to do is make this "Triangle" in python with for loops:

Pascal

But with the text like so:

0|0
1|01
2|012
3|0123
4|01234

What the output that I currently have is:

0|01234
1|01234
2|01234
3|01234
4|01234

And here is the code for the output:

def pascal(n):
    answer = ""
    for rows in range(n):
        answer = str(rows) + "|"
        for col in range(n):
            answer = answer + str(col)
        print(answer)

pascal(5)

My question is, how the heck do I make the ouput do this?(Im stumped as to what im supposed to do):

0|0
1|01
2|012
3|0123
4|01234



If anyone would like to see what the hell I was trying to accomplish, heres my solution

Soooooooo, this blue triangle:

Pascal

turns into the pascal triangle, by "n choose k":

formula

I was trying to figure out the for loops so I can have the basic setup done(which is the blue triangle), which you guys helped with :)

So the code that I came up with to get the n choose k is this:

def factorial(n):
    answer = 1
    for number in range(2, n+1):
       answer = answer * number
    return answer


def pascal(n):
    answer = ""
    for rows in range(n):
        answer = ""
        for col in range(rows+1):
            answer = answer + str( int(factorial(rows) / (factorial(col)*factorial(rows-(col)))) )

        print(answer)
pascal(10)

The factorial() is the exclamation point in the n choose k formula and I made the rest of the formula with this code:

factorial(rows) / (factorial(col)*factorial(rows-(col))) 

So any n that is greater than 0, makes a pascal triangle :)

2
  • 1
    Your code is fine, you only need to change this line: for col in range(n):. Shouldn't loop all the way to n Commented Nov 4, 2013 at 1:35
  • Actually, it should be range(rows+1). But, the entire outer loop body can be replaced with: print(rows, "|", *range(rows+1), sep="") Commented Nov 4, 2013 at 2:01

5 Answers 5

3

You are close. When printing each row you don't want to go all the way to n. The inner loop should stop at rows, so change for col in range(n) to for col in range(rows+1).

def pascal(n):
    answer = ""
    for rows in range(n):
        answer = str(rows) + "|"
        for col in range(rows+1):
            answer = answer + str(col)
        print(answer)

pascal(5)
Sign up to request clarification or add additional context in comments.

Comments

2

Just modify the second for

Code:

def pascal(n):
    answer = ""
    for rows in range(n):
        answer = str(rows) + "|"
        for col in range(rows + 1): # Modify this
            answer = answer + str(col)
        print(answer)

pascal(5)

Why? - Because your first for is for the number of lines, the second for is for the elements in that line, you don't want to loop over, let's say, 5 again, just over the correct number of elements, in this case, it will be the number of rows.

Comments

1

Your code was correct but your inner loop shouldn't loop to n. You should replace it with rows+1.

def pascal(n):
    answer = ""
    for rows in range(n):
        answer = str(rows) + "|"
        for col in range(rows+1):
            answer = answer + str(col)
        print(answer)

pascal(5)

output:

0|0
1|01
2|012
3|0123
4|01234

Comments

0

Honestly, I wouldn't use a for loop to do this. A while loop will be much more efficient for what you want to do. For instance:

pascal(n):
  answer=''
  answerright = ''
  i=0
  while i <= n:
    answerright += str(i)
    answer += str(i) + '|' + answerright + '\n'
    i+=1
  return answer

That will generate what you're looking for, without overly complicating the code. You don't say if you are required to use for loops, but if not, this is the better solution. In python, all for loops are really foreach loops, always remember that.

2 Comments

Why I was using for loops, I have no idea. I think I was just trying to get the triangle down, like the (0/0) so I can put that into a 0,0 form to get it to use the "n choose k" sequance to make the pascal triangle. if that doesnt make sense is because i am still trying to figure it out myself.
Your code is more efficient because you omitted one of the two loops, but the loop type doesn't matter much. In your code it's perfectly acceptable to interchange the while-loop for a for-loop; it would be faster and arguably cleaner as well.
0

This should get you on the right path:

for row in range (5):
  row_string = str(row) + "|"

  for i in range(row+1):
    row_string += str(i)

  print row_string

You only want to iterate up to the current row (+1 since we're zero-indexed here) in your inner for-loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.