Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python的while循环对金字塔进行编号_Python_While Loop_Nested - Fatal编程技术网

使用python的while循环对金字塔进行编号

使用python的while循环对金字塔进行编号,python,while-loop,nested,Python,While Loop,Nested,我应该有这样一个输出: 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 6 5 4 3 2 1 我正在使用以下代码: outer = 1 while outer <=6: inner = 1 while inner <= outer: print(inner, end=" ") inner = inner + 1 print(" ")

我应该有这样一个输出:

          1
        2 1
      3 2 1
    4 3 2 1
  5 4 3 2 1
6 5 4 3 2 1
我正在使用以下代码:

 outer = 1
 while outer <=6:
     inner = 1
     while inner <= outer:
         print(inner, end=" ")
         inner = inner + 1
     print(" ")
     outer = outer + 1
有人能告诉我我做错了什么吗?

使用
string.join()

制造

使用
string.join()

制造


查看您的代码,只有两个问题: 1.您正在按递增的顺序打印。因此,如果你从内部减去外部,它将按降序打印数字 2.填充从一行到下一行各不相同,因此在外部循环中添加填充。这是您的代码在2个修订版中的外观:

outer = 1
while outer <=6:
     inner = 1
     print (" " * 2 *(6 - outer), end="")
     while inner <= outer:
         print(outer - inner + 1, end=" ")
         inner = inner + 1
     print(" ")
     outer = outer + 1
outer=1

查看代码时,只有两个问题: 1.您正在按递增的顺序打印。因此,如果你从内部减去外部,它将按降序打印数字 2.填充从一行到下一行各不相同,因此在外部循环中添加填充。这是您的代码在2个修订版中的外观:

outer = 1
while outer <=6:
     inner = 1
     print (" " * 2 *(6 - outer), end="")
     while inner <= outer:
         print(outer - inner + 1, end=" ")
         inner = inner + 1
     print(" ")
     outer = outer + 1
outer=1

而外部另一个
while
解决方案:

outer = 1
while outer <= 6:
    inner = outer
    pos = 6       # create a position variable and start from 6 since you are printing backward
    while pos >= 1:
        if pos > inner:         # print white space if pos is larger than the diagonal
            print(" ", end=" ")
            pos = pos - 1
        else:
            print(inner, end = " ")  # Start to print number when pos is smaller than inner
            inner = inner - 1
            pos = pos - 1
    print(" ")
    outer = outer + 1

          1  
        2 1  
      3 2 1  
    4 3 2 1  
  5 4 3 2 1  
6 5 4 3 2 1 
outer=1
当外部=1时:
如果位置>内部:#如果位置大于对角线,则打印空白
打印(“,end=”“)
位置=位置-1
其他:
打印(内部,end=”“)#当pos小于内部时开始打印号码
内部=内部-1
位置=位置-1
打印(“”)
外部=外部+1
1.
2 1  
3 2 1  
4 3 2 1  
5 4 3 2 1  
6 5 4 3 2 1 

另一个
解决方案:

outer = 1
while outer <= 6:
    inner = outer
    pos = 6       # create a position variable and start from 6 since you are printing backward
    while pos >= 1:
        if pos > inner:         # print white space if pos is larger than the diagonal
            print(" ", end=" ")
            pos = pos - 1
        else:
            print(inner, end = " ")  # Start to print number when pos is smaller than inner
            inner = inner - 1
            pos = pos - 1
    print(" ")
    outer = outer + 1

          1  
        2 1  
      3 2 1  
    4 3 2 1  
  5 4 3 2 1  
6 5 4 3 2 1 
outer=1
当外部=1时:
如果位置>内部:#如果位置大于对角线,则打印空白
打印(“,end=”“)
位置=位置-1
其他:
打印(内部,end=”“)#当pos小于内部时开始打印号码
内部=内部-1
位置=位置-1
打印(“”)
外部=外部+1
1.
2 1  
3 2 1  
4 3 2 1  
5 4 3 2 1  
6 5 4 3 2 1 

啊,也非常感谢您的解释。这真的帮了我很多^ ^啊,也非常感谢你的解释。这真的帮了我很大的忙^_^
outer = 1
while outer <= 6:
    inner = outer
    pos = 6       # create a position variable and start from 6 since you are printing backward
    while pos >= 1:
        if pos > inner:         # print white space if pos is larger than the diagonal
            print(" ", end=" ")
            pos = pos - 1
        else:
            print(inner, end = " ")  # Start to print number when pos is smaller than inner
            inner = inner - 1
            pos = pos - 1
    print(" ")
    outer = outer + 1

          1  
        2 1  
      3 2 1  
    4 3 2 1  
  5 4 3 2 1  
6 5 4 3 2 1