有人能解释如何在python中使用while循环创建金字塔吗?
userInput = int(input("Please enter the amount of rows: "))
row = 0
while(row < userInput):
row += 1
spaces = userInput - row
spaces_counter = 0
while(spaces_counter < spaces):
print(" ", end='')
spaces_counter += 1
num_stars = 2*row-1
while(num_stars > 0):
print("*", end='')
num_stars -= 1
print()
如果有人向我解释那个代码,那就太过分了
您只需要修复缩进:
userInput = int(input("Please enter the amount of rows: "))
row = 0
while(row < userInput):
row += 1
spaces = userInput - row
spaces_counter = 0
while(spaces_counter < spaces):
print(" ", end='')
spaces_counter += 1
num_stars = 2*row-1
while(num_stars > 0):
print("*", end='')
num_stars -= 1
print() # next line
输出
Please enter the amount of rows: 5
*
***
*****
*******
*********