PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#Prints out a number triangle of size N, N being user given input. Also prints out the sum of all numbers in the traingle.
import sys
try:
size = int(input("Enter the size of the triangle: "))
except ValueError:
print("\n\nInvalid input.")
sys.exit()
print("\n\n")
summation = 0
for i in range(0, size):
row = ""
for j in range(0, size-i+1):
row += " "
for k in range(0, i+1):
summation += k+1
row += str(k+1) + " "
print(row)
print("\nSum of triangle is: {0}".format(summation))
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run