Python:为什么函数的名称会在最后自动添加?

时间:2017-11-23 11:56:58

标签: python function

以下是我正在处理的代码:

def ligneComplete(x,y): #function to print a full line of #
   for loop in range(y):
      print(x, end = "")
   return ligneComplete

def ligneEspace(x,y,z): #function to print a ligne of # with space between
   for loop in range(z-2):
      print(x, end="")
      for loop in range(y-2):
         print(" ", end="")
      print(x)
   return ligneEspace 


x = "#"
z = int(input()) #nbcolonne
y = int(input()) #nbligne

print(ligneComplete(x,y)) #print a full ligne of #
print(ligneEspace(x,y,z)) #print ligne with space

#why there is a break line here???????

print(ligneComplete(x,y)) #print full ligne of #
print()

结果如下:

#####
#   #
#   #
#   #

#####

我想这样:

#####
#   #
#   #
#   #
#####

有人可以告诉我为什么在我的功能结束时有一个断线?我试图找到一些答案,但每个主题都是关于添加换行符而不是删除换行符。 非常感谢你的帮助。

3 个答案:

答案 0 :(得分:2)

您的代码中存在一些错误:

  • 您无需从这些功能中返回任何内容。
  • 您无需在这些功能上调用打印。
  • 在这些情况下,您不需要显式循环变量。
  • 第一个功能中缺少print()
  • 正如AK47所说,你所声称的实际输出并非我所看到的。

你的功能应该是这样的:

def ligneComplete(x,y): #function to print a full line of #
   for _ in range(y):
      print(x, end = "")
   print()

def ligneEspace(x,y,z): #function to print a ligne of # with space between
   for _ in range(z-2):
      print(x, end="")
      for _ in range(y-2):
         print(" ", end="")
      print(x)


x = "#"
z = 5 #int(input()) #nbcolonne
y = 5 #int(input()) #nbligne

ligneComplete(x,y) #print a full ligne of #
ligneEspace(x,y,z) #print ligne with space
ligneComplete(x,y) #print full ligne of #

输出:

#####
#   #
#   #
#   #
#####

答案 1 :(得分:0)

看起来你不想在打电话时打印这些功能。这些函数自己调用print()。

ligneComplete(x,y) #print a full ligne of #
ligneEspace(x,y,z) #print ligne with space

#why there is a break line here???????

ligneComplete(x,y) #print full ligne of #
print()

答案 2 :(得分:0)

def ligneComplete(x,y): #function to print a full line of #
   for loop in range(y):
      print(x, end = "")

def ligneEspace(x,y,z): #function to print a ligne of # with space between
   for loop in range(z-2):
      print(x, end="")
      for loop in range(y-2):
         print(" ", end="")
      print(x, end='')


x = "#"
z = int(input()) #nbcolonne
y = int(input()) #nbligne

ligneComplete(x,y) #print a full ligne of #
ligneEspace(x,y,z) #print ligne with space

ligneComplete(x,y) #print full ligne of #

print()

会奏效。