2

我有一个 python 程序可以生成一个 triforce,但是在中间,在循环之间,添加了一个新行。我怎样才能删除这个?

源代码:

var = 10

for x in range(var+1):
    print(' '*x+'v'*(10-x)*2+' '*x+' '*x+'v'*(10-x)*2)
for p in range(var+1):
    print(' '*var+' '*p+'v'*(var-p)*2)

input()

结果:

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
 vvvvvvvvvvvvvvvvvv  vvvvvvvvvvvvvvvvvv
  vvvvvvvvvvvvvvvv    vvvvvvvvvvvvvvvv
   vvvvvvvvvvvvvv      vvvvvvvvvvvvvv
    vvvvvvvvvvvv        vvvvvvvvvvvv
     vvvvvvvvvv          vvvvvvvvvv
      vvvvvvvv            vvvvvvvv
       vvvvvv              vvvvvv
        vvvv                vvvv
         vv                  vv

          vvvvvvvvvvvvvvvvvvvv
           vvvvvvvvvvvvvvvvvv
            vvvvvvvvvvvvvvvv
             vvvvvvvvvvvvvv
              vvvvvvvvvvvv
               vvvvvvvvvv
                vvvvvvvv
                 vvvvvv
                  vvvv
                   vv

我想消除中间的那个间隙,但.rstrip('\n')不起作用。

谢谢。

4

4 回答 4

2
var = 10

for x in range(var):
    print(' '*x+'v'*(10-x)*2+' '*x+' '*x+'v'*(10-x)*2)
for p in range(var+1):
    print(' '*var+' '*p+'v'*(var-p)*2)

input()

这个可以吗?

于 2013-05-15T00:39:07.180 回答
2

它不是换行符。x 使其变为 10,因此您将在 for 循环中插入一个空行

于 2013-05-15T00:41:46.050 回答
2

正如@ValekHalfHeart 所述,换行符是在第一个块中创建的

您可以通过更改第一个块来修复,如下所示:

for x in range(var):
    print(' '*x+'v'*(var-x)*2+' '*x+' '*x+'v'*(var-x)*2)

或者如果您想保留 (var+1),请执行以下操作:

for x in range(var+1):
    print(' '*x+'v'*(var+1-x)*2+' '*x+' '*x+'v'*(var+1-x)*2)

问题是10 - 10 = 0在第一个循环的最后一次迭代中,你得到一行''字符而没有'v'。

于 2013-05-15T00:42:18.880 回答
0

尖尖的替代方案

var = 10

for x in range(var):
    print(' '*x+'v'+'v'*(var-1-x)*2+' '*(2*x+1)+'v'+'v'*(var-1-x)*2)

for p in range(var):
    print(' '*var+' '*p+'v'+'v'*(var-1-p)*2)

印刷...

vvvvvvvvvvvvvvvvvvv vvvvvvvvvvvvvvvvvvv
 vvvvvvvvvvvvvvvvv   vvvvvvvvvvvvvvvvv
  vvvvvvvvvvvvvvv     vvvvvvvvvvvvvvv
   vvvvvvvvvvvvv       vvvvvvvvvvvvv
    vvvvvvvvvvv         vvvvvvvvvvv
     vvvvvvvvv           vvvvvvvvv
      vvvvvvv             vvvvvvv
       vvvvv               vvvvv
        vvv                 vvv
         v                   v
          vvvvvvvvvvvvvvvvvvv
           vvvvvvvvvvvvvvvvv
            vvvvvvvvvvvvvvv
             vvvvvvvvvvvvv
              vvvvvvvvvvv
               vvvvvvvvv
                vvvvvvv
                 vvvvv
                  vvv
                   v

表达式可以简化(数学上),但想法是打印:

  • 缩进
  • 三角形
  • v的列
  • 第一个对称三角形

在上半部分,它是双倍的,第二部分的缩进也考虑到前一部分。下部有更简单的压痕。

于 2013-05-15T08:01:17.747 回答