我在 Python 中的模式匹配问题之一中苦苦挣扎
当 input = 3 时,下面是预期的输出(输入值是它应该打印的列数)
预期输出:
1
2 6
3 7 9
4 8
5
Run Code Online (Sandbox Code Playgroud)
我不知何故朝着错误的方向前进,因此需要一些帮助。
这是我到目前为止尝试过的代码:
def display():
n = 5
i = 1
# Outer loop for how many lines we want to print
while(i<=n):
k = i
j = 1
# Inner loop for printing natural number
while(j <= i):
print (k,end=" ")
# Logic to print natural value column-wise
k = k + n - j
j = j + 1
print("\r")
i …Run Code Online (Sandbox Code Playgroud)