Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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/9/loops/2.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 创建图案的嵌套循环_Python_Loops_Nested - Fatal编程技术网

Python 创建图案的嵌套循环

Python 创建图案的嵌套循环,python,loops,nested,Python,Loops,Nested,如何使用嵌套循环创建以下模式 111111 11111 1111 111 11 1 111111 11111 1111 111 11 1 到目前为止,我有这个,我似乎被卡住了 def main(): stars = "******" for x in range (1,7): print(stars) for y in range (1,1): stars = stars.replace("*"," ") main() 只需更换内循环中的一颗星: star

如何使用嵌套循环创建以下模式

111111
11111
1111
111
11
1
111111
11111
1111
111
11
1
到目前为止,我有这个,我似乎被卡住了

def main():
stars = "******"
for x in range (1,7):
    print(stars)

    for y in range (1,1):
        stars = stars.replace("*"," ")
main()

只需更换内循环中的一颗星:

stars = "******"
for x in range(6): 
    stars = stars.replace("*","1")
    print(stars)
    for y in range(1): # need range(1) to loop exactly once
        stars = stars.replace("1","",1) 
for x in range(5, -1, -1):
    print("1" * x, end="")
    for y in range(1):
        print("1")
输出:

111111
11111
1111
111
11
1
******
*****
****
***
**
*
111111
11111
1111
111
11
1
如果你真的想要星星:

stars = "******"
for x in range(6):
    print(stars)
    for y in range(1):
        stars = stars.replace("*","",1)
输出:

111111
11111
1111
111
11
1
******
*****
****
***
**
*
111111
11111
1111
111
11
1
最后一个参数是count,其中仅替换第一个计数出现。所以每次我们只替换一个字符

如果必须使用stars变量并替换,则上述代码将起作用。如果只需要嵌套循环并创建图案,则可以从5开始循环,并在内部循环中使用
end=“”
打印一次:

stars = "******"
for x in range(6): 
    stars = stars.replace("*","1")
    print(stars)
    for y in range(1): # need range(1) to loop exactly once
        stars = stars.replace("1","",1) 
for x in range(5, -1, -1):
    print("1" * x, end="")
    for y in range(1):
        print("1")
同样的输出:

111111
11111
1111
111
11
1

您正在使用replace方法替换字符串中的所有星星,这意味着您将在线打印一行start。您可以使用substring方法获得更好的结果。

您可以使用简单的方法获得相同的输出,因为Python支持字符串上的
*
运算符,该运算符返回重复出现的字符串

character = "1"   #You can change it to "*"
for i in range(6, 0, -1):
    print character*i
输出:

111111
11111
1111
111
11
1
******
*****
****
***
**
*
111111
11111
1111
111
11
1

用嵌套循环检查Padraic Cunningham的答案

不带嵌套循环:

  • 按列表的范围方法和反转方法创建计数列表
  • 迭代列表并打印计数的1倍
  • 代码:

    输出:

    111111
    11111
    1111
    111
    11
    1
    
    ******
    *****
    ****
    ***
    **
    *
    
    111111
    11111
    1111
    111
    11
    1
    

    您可以为参数指定一个符号(示例-'1'、'*')和数字(示例-'5代表******'

    因为您请求的是嵌套循环,我想这只是一个训练练习,效率如何并不重要。
    因此,让我提出一个具有“适当”内环且无反向范围的解决方案:

    def triangle(x, c='1'):
        for i in range(x):
            line = ''
            for _ in range(i, x):
                line += c
            print(line)
    

    非常感谢你。我知道这不难。@prsn,不用担心,很高兴它能帮上忙内环基本上是无环的。。。它只运行一次,因此您也可以不使用它,直接执行它的内容。@swenzel。那么它就不会嵌套了,请阅读问题。无论循环运行一次还是1000000次,它仍然是一个问题loop@PadraicCunningham我同意,这在形式上是一个循环。这就是我为什么说基本上。但因为它是预先确定的,它只运行一次,所以我认为它是在欺骗,只有在那里,你才能说:“它是嵌套的”。请不要认为这是冒犯,这只是批评:)这不是一个错误loop@PadraicCunningham:是的,没错。我再一次没有仔细阅读这个问题。道歉。但是我们直接使用
    stars=stars.replace(“*”,“”,1)
    ,因为每秒钟
    for循环
    只运行一次。向上投票给你+1是的,在一个循环中做这件事很简单,我认为挑战在于在两个循环中做这件事,并使用replace。@PadraicCunningham:有问题,
    stars='*****'
    然后我做
    stars。replace(“,“V”)
    然后我得到了
    V*V*V*V*V*V
    为什么输出中有
    V
    ?你用V替换了一个空字符串。