我的教授在课堂上分享了这段代码,我真的不明白。任何人都可以向我解释这个程序究竟发生了什么?
#Task 1: Prompt the user to input the number of rows of the triangle.
rows = eval(input("How many rows should the equilateral triangle have?"))
#Task 2: Calculate how many asterisks in the last row, write outer loop.
for i in range(rows + 1):
#Task 3: For each outer loop, calculate how many spaces and asterisks need to be printed in each row.
emptySpaces = rows - i
#Task 4: Write inner loop to print spaces and asterisks.
print(' ' * emptySpaces + '* ' * i)
这就是输出的样子
How many rows should the equilateral triangle have?6
*
* *
* * *
* * * *
* * * * *
* * * * * *
答案 0 :(得分:0)
这是该计划的方式:
开始:强>
循环:
循环执行x + 1次,程序输出x + 1行。第一个是空的,最后一个是x' *' s。
这就是全部。