0

I have created a Pascal's Triangle. Everything works how I want it to. I now want to change the color of specific numbers in the triangle. I would like to have the diagonals different colors, ie: The counting numbers would be blue and the triangular numbers would be orange. Is there a way that I can change the color of numbers in different lines, or even set up a sequence of colors (each number in a line is blue, red, orange, yellow.....then the following like would restart the sequence but at red)?

# Pascal's Triangle

def multiplicative(n, k):
    result = 1
    for i in range(1, k+1):
        result = result * (n-(k-i))/i
    return result

def pascal_level(n):
    lists = []
    ns = range(n)
    for n in ns:
        nlist = []
        for k in range(n+1):
            nlist.append(multiplicative(n, k))
        nlist = '     '.join(map(str, nlist))
        lists.append(nlist)
    return lists

def center(strings):
    maxlistlen = len(max(strings, key=len))
    for i, s in enumerate(strings):
        diff = maxlistlen - len(s)
        pad = ' '*(diff/2)
        yield str(i)+'-> '+pad+s

answer = int(raw_input("How many levels do you want? \n"))
ps = pascal_level(answer)
for r in center(ps):
     print r

edit - I have a limited understanding of this so I will have to play around with the suggestions. Thanks for the help and I'll post an update when I finish. I've added a picture of something similar to what I'm trying to create.Example

  • 2
    Your question has nothing to do with `pascal` the programming language, so I removed that tag. Please read the tag description before adding. – Tom Brunberg Dec 17 '17 at 07:27
  • 1
    A solution without additionals libraries: [How do I change the color of a specific printed letter in python?](https://stackoverflow.com/questions/47670946/how-do-i-change-the-color-of-a-specific-printed-letter-in-python) – Mr. T Dec 17 '17 at 09:14
  • @Piinthesky I've read that thread, but that is for changing the color of an entire sting, or at least that's how I interpreted it. – James Snead Dec 17 '17 at 15:42
  • print("\033[34mN" + "\033[0mot\033[32m " + "r\033[0meally") – Mr. T Dec 17 '17 at 15:54

2 Answers2

1

You can use a library like colored to change both foreground and background colors, even on the same line.

Something like:

from colored import stylize, fg

offset = 42  # Starting color
# ...

def pascal_level(n):
    lists = []
    ns = range(n)
    for n in ns:
        nlist = []
        colors = []
        for k in range(n+1):
            nlist.append(multiplicative(n, k))
            color = fg(k + offset) if k + 1 < n//2 else fg(n - k + offset)
            colors.append(color)
        nlist = '     '.join(stylize(str(x), color) for x, color in zip(nlist, colors))
        lists.append(nlist)
    return lists
Thomas Fauskanger
  • 2,536
  • 1
  • 27
  • 42
1

This has been answered previously, colorama

Change color of individual print line in Python 3.2? [duplicate]

colorama also works in 2.7

ShpielMeister
  • 1,417
  • 10
  • 23