2

I have a python program that makes a triforce, but in the middle, between loops, a new line is appended. How can I remove this?

Source code:

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()

Result:

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

I would like to remove that gap in the middle, but .rstrip('\n') doesn't work.

Thanks.

timss
  • 9,982
  • 4
  • 34
  • 56
ToXic73
  • 332
  • 6
  • 15

4 Answers4

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()

Is this ok?

igon
  • 3,016
  • 1
  • 22
  • 37
2

It's not a newline character. x makes it to 10, so you are inserting a blank line with your for loop

mrKelley
  • 3,365
  • 2
  • 21
  • 28
2

As stated by @ValekHalfHeart, the newline is created in the first block

You can fix by changing the first block as follows:

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

or if you'd like to keep the (var+1), then do the following:

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

The issue is that 10 - 10 = 0 on that last iteration of the first loop, so you get a line of ' ' characters and no 'v's.

mattc
  • 485
  • 3
  • 9
0

The alternative vith the sharp tip

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)

prints...

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

The expressions can be simplified (mathematically), but the idea is to print:

  • indentation
  • triangle
  • column of v's
  • symmetric triangle to the first one

In the upper part it is doubled, the indentation of the second part counts into account also the previous part. The lower part has simpler indentation.

pepr
  • 20,112
  • 15
  • 76
  • 139