I finished the code and it worked with the value that i put in. but i just dont know how to make it return in a form like pascal triangle.
import math
n=int(input("choose your whole number"))
def J(n,r):
return math.factorial (n)//(math.factorial (n-r)*math.factorial (r))
def G(z):
for n in range (z+1):
for r in range (n+1):
print (J(n,r), end=" ")
G(n)
The answer is very simple: you need a newline after you finish printing the numbers on a line. Just add an empty print statement (careful with the indentation):
def G(z):
for n in range (z+1):
for r in range (n+1):
print (J(n,r), end=" ")
print()
You need to add a newline after printing each new row. If you want to pad the top of the pyramide (so it looks like a balanced pyramide) you need to print an offset as well:
def G(z):
for n in range (z+1):
# Pad the numbers so they appear in a pyramide
print(' ' * (z-n), end="")
for r in range (n+1):
# Add the ljust-bit to make sure the numbers
# gets an even spacing
print (str(J(n,r)).ljust(4), end="")
# New line after each row
print()
Example:
choose your whole number 5
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.