简体   繁体   中英

Why am I getting spaces after every line in my python code?

So, I was solving this problem on a competitive coding site, and it is not accepting my answer, I figured this is because my output is giving space after every line.

I want to know why this space is coming and what I can do to change my code(Want to know what I am doing wrong.) so that it does not leave a line.

Here's the problem:

Mr. Vincent works in a door mat manufacturing company. One day, he designed a new door mat with the following specifications:

Mat size must be NxM. (N is an odd natural number, and M is 3 times N.) The design should have 'WELCOME' written in the center. The design pattern should only use |, . and - characters.

Sample Designs:

Size: 7 x 21 
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------

Input Format:

A single line containing the space separated values of N and M. Also, M is always 3 times N

Output Format:

Output the design pattern.

This is my code:

data = input().split()
height = int(data[0])
width = int(data[1])

pattern = ''

def methodA(times):
    print("-"*times,end = '')

def methodB(times):
    print(".|."*times, end = '')

inputDecrease = (width - 3)//2
inputIncrease = 1

#upperHalfStarts

for i in range(0, height//2 ):
    methodA(inputDecrease)
    methodB(inputIncrease)
    methodA(inputDecrease)
    print("\n")
    if not i == (height//2-1):
        inputDecrease -= 3
        inputIncrease += 2

#MiddleLineStarts

temp = (width -7)//2

methodA(temp)
print("WELCOME", end = '')
methodA(temp)
print("\n")

#lowerHalfStarts

for i in range(0, height//2):
    methodA(inputDecrease)
    methodB(inputIncrease)
    methodA(inputDecrease)
    print("\n")
    inputDecrease += 3
    inputIncrease -= 2

This is my output:

---------.|.---------

------.|..|..|.------

---.|..|..|..|..|.---

-------WELCOME-------

---.|..|..|..|..|.---

------.|..|..|.------

---------.|.---------

Expected Output:

---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------

print() already includes a line break at the end of the line. Doing print("\\n") means you print an explicit line break, plus the implicit one to end the output. You effectively run this:

print("\n", end="\n")

Use a plain print() instead, without the explicit newline.

    # Enter your code here. Read input from STDIN. Print output to STDOUT
lstr = '.|.'
varbl=input().split()
midString = 'WELCOME'
breakpt = (int(varbl[0])-2)//2

for i in range(int(varbl[0])):

    print((lstr*((2*i)+1)).center(int(varbl[1]),'-'))
    if i == breakpt:
        print(midString.center(int(varbl[1]),'-'))
        break 
       
i = int(varbl[0])//2          
while i > 0:
    pattern = lstr * ((2 * i) - 1) 
    print (pattern.center (int(varbl[1]), '-')) 
    i = i-1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM