chevron_left chevron_right
Login Register invert_colors photo_library


Stay updated and chat with others! - Join the Discord!
Thread Rating:
  • 0 Vote(s) - 0 Average


Pascal Triangle Generator (Python 3.3) filter_list
Author
Message
Pascal Triangle Generator (Python 3.3) #1
On my way to improving my Python, I made this:

Code:
import sys
import random
from math import factorial

f = factorial

def doTriangle(t):
    for a in range(0, t + 1):
        for b in range(0, a + 1):
            x = f(a) / (f(b) * f(a - b))
            print(int(x),end=" ")
        print("")
    print("Done!")

def main():
    try:
        if len(sys.argv) == 2:
            doTriangle(int(sys.argv[1]))
        else:
            doTriangle(int(input("Compute up to: ")))
    except ValueError:
        x = int(random.randrange(2, 20))
        print("Invalid input, randomly chose " + str(x) + ".")
        doTriangle(x)

main()

Pastebin link and Hastebin link for an easier way to read it.
[Image: CDUAq9d.png]

Reply






Users browsing this thread: 1 Guest(s)