I have been working a number pyramid program. This is a question from Y. Daniel Laing's Introduction to programming using Python (which is based on Python 3.2). The question is from chapter 5, and it is number 5.19 in programming exercises portion of the chapter. The question is stated as follows:
Write a program that prompts the user to enter an integer from 1 to 15 and displays it as the following sample run:
I don't have the image, but it is something like this:
Enter the number of lines: 3
1 2 1 2 3 2 1 2 3
The solution I have come up with is this:
p = eval(input("Enter the number of lines: "))
num = 0
y = 1
while num < p:
y = num
line = str(y+1) + " "
while y >= 1:
y -= 1
line += str(y+1) + " "
while y < num:
line += str(y + 2) + " "
y +=1
print(format(line, "^80s"))
num +=1
If anyone has a better solution that would be simpler, please let me know.