为什么python代码中的每一行后面都有空格?
所以,我在一个有竞争力的编码网站上解决了这个问题,它不接受我的答案,我想这是因为我的输出在每一行后面都有空间 我想知道为什么这个空间会出现,我能做些什么来改变我的代码(想知道我做错了什么),这样它就不会留下一行 问题是: 文森特先生在一家门垫制造公司工作。一天,他设计了一种新的门垫,规格如下: 垫子尺寸必须为NxM。(N是奇数自然数,M是N的3倍。) 设计应该在中心写上“欢迎”。 设计模式应仅使用|、。和-字符 样本设计:为什么python代码中的每一行后面都有空格?,python,python-3.x,Python,Python 3.x,所以,我在一个有竞争力的编码网站上解决了这个问题,它不接受我的答案,我想这是因为我的输出在每一行后面都有空间 我想知道为什么这个空间会出现,我能做些什么来改变我的代码(想知道我做错了什么),这样它就不会留下一行 问题是: 文森特先生在一家门垫制造公司工作。一天,他设计了一种新的门垫,规格如下: 垫子尺寸必须为NxM。(N是奇数自然数,M是N的3倍。) 设计应该在中心写上“欢迎”。 设计模式应仅使用|、。和-字符 样本设计: Size: 7 x 21 ---------.|.---------
Size: 7 x 21
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
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
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
输入格式:
Size: 7 x 21
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
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
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
包含N和M的空间分隔值的单行。
而且,M总是N的3倍
输出格式:
Size: 7 x 21
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
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
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
输出设计模式
这是我的代码:
Size: 7 x 21
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
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
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
这是我的输出:
Size: 7 x 21
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
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
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
预期输出:
Size: 7 x 21
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
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
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
print()
已在该行的末尾包含换行符。执行打印(“\n”)
意味着您打印一个显式换行符,加上隐式换行符以结束输出。您可以有效地运行以下命令:
print("\n", end="\n")
使用普通的print()
,而不使用显式换行符。可能是print(“\n”,end=”)
或者只是print()
我用print(“”)替换了所有的打印(\n”),它可以工作。谢谢请注意,您甚至可以使用print()
(里面没有空字符串)。如果答案解决了您的问题,请接受(绿色复选标记)。
# 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