本文介绍了Python:打印星号的三角形图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用嵌套for循环和 print('*',end ='')来创建显示的模式:

这里是我的代码。

  n = 0 

print(模式A)
在范围内(0,11):
n = n + 1
(0,n-1):
print('*',end = '')
print()
print('')
print(Pattern B)
for b in range(0,11):
n = n - 1
for d(0,n + 1):
print('*',end ='')
print()
print('')

当我启动模式C和D时,我尝试以下操作:

$ p $ print(C模式)
在范围内(11,0,-1):
n = n + 1 $ b $ (0,n + 1):b $ b print('*',end ='')
print()
print('')
print(' (0,n-1):
在范围(11,0, print('*',end ='')
print()

和A和B是一样的编辑!

解决方案

模式C和D都需要前导空格,并且不打印任何空格,只是星号。



下面是可以打印所需前导空格的替代代码:

  print( C)
(11,0,-1):
print((11-e)*''+ e *'*')

print ('')
print(模式D)
在g范围内(11,0,-1):
print(g *''+(11-g)*' *')

以下是输出:

 模式C 
***********
**********
**** *****
********
*******
******
*****
****
***
**
*

模式D

*
**
***
****
*****
******
*******
*** *****
*********
**********

中更多细节,请考虑以下行:

  print((11-e)*''+ e *'*')

打印(11-e)空格, e 星星。如果您的老师需要嵌套循环,那么您可能需要将 print((11- e)*''+ e *'*')循环打印每个空间,一次一个,然后是每个星星,一次一个。

模式C通过嵌套循环



如果你遵循了我给出的关于嵌套循环的提示,你将会得到如下模式C的解决方案:



$ p $ print(模式C)
在范围内(11,0,-1):
$ print((11-e)*''+ e *'*')
在范围(11-e)中的d:
print('',end ='')$ b (e):
print('*',end ='')
print()


I am required to use nested for loops and print('*', end=' ') to create the pattern shown here:

And here is my code. I have figured out the first two.

n = 0

print ("Pattern A")
for x in range (0,11):
    n = n + 1
    for a in range (0, n-1):
        print ('*', end = '')
    print()
print ('')
print ("Pattern B")
for b in range (0,11):
    n = n - 1
    for d in range (0, n+1):
        print ('*', end = '')
    print()
print ('')

When i start pattern C and D, i try the following:

print ("Pattern C")
for e in range (11,0,-1):
    n = n + 1
    for f in range (0, n+1):
        print ('*', end = '')
    print()
print ('')
print ("Pattern D")
for g in range (11,0,-1):
    n = n - 1
    for h in range (0, n-1):
        print ('*', end = '')
    print()

But the result is the same as A and B. Help is appreciated!

解决方案

Both patterns C and D require leading spaces and you are not printing any spaces, just stars.

Here is alternative code that prints the required leading spaces:

print ("Pattern C")
for e in range (11,0,-1):
    print((11-e) * ' ' + e * '*')

print ('')
print ("Pattern D")
for g in range (11,0,-1):
    print(g * ' ' + (11-g) * '*')

Here is the output:

Pattern C
***********
 **********
  *********
   ********
    *******
     ******
      *****
       ****
        ***
         **
          *

Pattern D

          *
         **
        ***
       ****
      *****
     ******
    *******
   ********
  *********
 **********

In more detail, consider this line:

print((11-e) * ' ' + e * '*')

It prints (11-e) spaces followed by e stars. This provides the leading spaces needed to make the patterns.

If your teacher wants nested loops, then you may need to convert print((11-e) * ' ' + e * '*') into loops printing each space, one at a time, followed by each star, one at a time.

Pattern C via nested loops

If you followed the hints I gave about nested loops, you would have arrived at a solution for Pattern C like the following:

print ("Pattern C")
for e in range (11,0,-1):
    #print((11-e) * ' ' + e * '*')
    for d in range (11-e):
        print (' ', end = '')
    for d in range (e):
        print ('*', end = '')
    print()

这篇关于Python:打印星号的三角形图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:23