教授要求在python中编写这个程序:

时间:2017-10-24 02:41:43

标签: python loops nested

教授要求在python中编写这个程序:

问题
编写一个程序来提示三角形的数量和每个三角形的高度,然后打印出所有指定的三角形。

三角形应具有以下格式:

How many triangles: 2

The height of the triangle: 3

1  3     5  
 7     9   
 11   

The height of the triangle: 4

1     3     5     7   
9    11    13   
15    17   
 19   

Process finished with exit code 0


How many triangles: 3

The height of the triangle: 1

1   

The height of the triangle: 3

1     3     5   
  7     9   
 11   

The height of the triangle: 5

 1     3     5     7     9   
 11    13    15    17   
 19    21    23   
 25    27   
 29   

Process finished with exit code 0

到目前为止我得到了这个:

number = eval(input("Enter height"))

for i in range(1, number+1):
    for j in range(i, 10):
        print(j+i, end="\t")
    print("\n")
    print()

1 个答案:

答案 0 :(得分:0)

你需要以相反的顺序取得范围:

number = int(input(("Height of triangle : ")))
odd_number = 1
for i in range(number, 0, -1):
    for j in range(0, i):
        print("{}".format(odd_number), end="\t")
        odd_number += 2
    print("\n")