有人可以帮忙找到一种方法来制作带有嵌套for循环的梯形吗? 我做到了,但它不断使需要的星星更多。有人可以给我建议。
import time
row1 = int(input("What is the length of the top side: "))
star = ("*"*row1)
height = int(input("Enter the height of this trapezoid:"))
for row in range(1, height + 1):`
# print out the right number of spaces
for spaces in range(1, height - row + 1):
print(" ", end = "")
time.sleep(0.5)
for asterisks in range(row):
print(star, end = "" )
print()
答案 0 :(得分:0)
您的代码有缺陷,您多次打印星形变量,因此开始次数过多,您需要将星形数增加2,而不是将星形数相乘。
for row in range(height):
# print out the right number of spaces
for spaces in range(height - row-1):
print(" ", end = "")
time.sleep(0.5)
for asterisks in range(row1+row*2):
print('*',end = "" )
print()
如果不需要time.sleep(),则可以在一个循环中完成。
for row in range(height):
print(" "*(height-row-1),"*"*(row1+row*2))