2

我正在尝试创建一个程序,允许我在 Python 程序中使用星号制作“金字塔”或“三角形”。我已经开始了代码,但似乎无法弄清楚。

这是我设法弄清楚的代码:

    def triangle():
            totalRows = int(eval(input("How big? ")))
            for currentRows in range(1,totalRows+1):
                for currentCol in range (1, currentRows+1):
                            print("*", end = " ")
    triangle()

最终结果应该反映这一点!

How big? 1
* 
------------------------------------------------------
How big? 2
 * 
* * 
------------------------------------------------------ 
How big? 3
  * 
 * * 
* * * 
------------------------------------------------------ 
4

10 回答 10

5

对 RParadox 的解决方案稍作修改以符合要求:

for i in range(totalRows + 1):
    numWhite = totalRows - i
    print ' ' * numWhite + '* ' * i
于 2012-11-06T23:18:11.937 回答
2
n = 10                                                                          
for i in range(n-1):                                                            
    numwhite = n-i                                                              
    print ' '*numwhite + '*'*i + '*'*i  



         **
        ****
       ******
      ********
     **********
    ************
   **************
  ****************
于 2012-11-06T22:47:55.613 回答
1
    def build_pyr(depth):
            rows = [ (depth-i)*' ' + i*2*'*' + '*'   for i in range(depth-1) ]
            for i in rows:
                    print i

这是可行的,但是,它会在每个深度添加 2n + 1 个星号。只需从列表理解中删除 + 1 星号,并事先在行列表中添加一个初始星号。

于 2012-11-06T23:41:06.207 回答
1
kiko="*"

empty=" "

def star(n):

    for i in range(n):
        print((n-i-1)*empty+(i+i+1)*kiko)
star(5)
于 2019-04-14T17:21:21.290 回答
0

只需几个步骤即可完成:

def main():

    rows = input(" enter the number of rows : ")
    for i in range (rows):
        print ' '*(rows-i-1) + '*'*(2*i+1)
main()
于 2014-01-12T06:52:31.577 回答
0
def xmastree(maxwidth):
    for i in xrange(1,maxwidth,2):
        print '{0}'.format('*'*i).center(maxwidth)

或者:

def xmastree2(maxrows):
    for i in xrange(1,maxrows*2,2):
        print '{0}'.format('*'*i).center(maxrows*2)

嗯,仍然没有回应,也许不够通用?好吧试试这个;-):

def triangle(pattern, n):   
    maxwidth = n*len(pattern)*2
    for i in xrange(1,n*2+1,2):
        print '{0}'.format(pattern*i).center(maxwidth)

>>> triangle(' ^', 5)
          ^         
        ^ ^ ^       
      ^ ^ ^ ^ ^     
    ^ ^ ^ ^ ^ ^ ^   
  ^ ^ ^ ^ ^ ^ ^ ^ ^ 
于 2012-11-06T23:37:19.960 回答
0

哇,伙计们,你从一个非常复杂的角度来看它!你为什么不直接使用这个程序?:

asterisknum = int(raw_input('How many asterisks? Input here: '))
base = 0
if asterisknum % 2 == 0:
  print ('Added 1 to even number')
  asterisknum = asterisknum + 1
while asterisknum != -1 :
  print (' ' * base + '*' * asterisknum)
  base = base + 1
  asterisknum = asterisknum - 2
raw_input('Press <Enter> to exit')

我只是让这个程序运行一次,但我使用了每个人都应该知道的极其简单的 python 部分。可以对其进行调整以在同一程序中再次工作,或者其他任何...

于 2013-12-18T02:15:05.963 回答
0
def triangle(height):
    maxstars = 1+ 2*(height -1)
    spaces = int(maxstars/2)
    for i in range(0,height):
        print("  " * spaces   +"*" * (1+ 2*i))
        spaces = spaces -1

number = int(input("Please enter height of triangle: "))
triangle(number)
于 2015-06-18T15:23:13.510 回答
0

想象一下,你有星星和空间的“块”。现在将它们添加到画布上。这是 Python。在这种特殊情况下,它将对象粘合在一起

此代码打印一个菱形,第一个循环是上半部分,第二个循环是下半部分。请注意,我必须为第二个循环创建新变量(对象)。

希望这对大家有帮助:)

star = "*"
space = "." # dots are for presentation purposes
rows = 10
star_counter = 1
space_counter = rows
for i in range(rows):
    print((space * (space_counter - 1 )) + (star * star_counter) +
    (star * (star_counter - 1)) + (space * (space_counter - 1)) )
    star_counter += 1
    space_counter -= 1

star_counter_new = (rows - 1) # one row less for the lower triangle
                             # the upper triangle takes the base row
                             # that's why rows - 1
space_counter_new = 1
for j in range(rows - 1): # same goes here
    print((space * (space_counter_new)) + (star * (star_counter_new)) +
    (star * (star_counter_new - 1)) + (space * (space_counter_new )))
    space_counter_new += 1
    star_counter_new -= 1
于 2015-08-27T21:57:27.773 回答
-1
def pypart2(n): 
    k = 2*n-2
    for i in range(0, n):  
        for j in range(0, k): 
            print(end=" ") 
        k=k-1
        for j in range(0, i+1):
            print("* ", end="")          
        print("\r") 


n = 5
pypart2(n) 
于 2019-08-20T13:12:09.843 回答