Python:为什么函数名会自动添加到末尾?

Python:为什么函数名会自动添加到末尾?,python,function,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):

以下是我正在编写的代码:

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()
结果如下:

#####
#   #
#   #
#   #

#####
#####
#   #
#   #
#   #
#####
我想这样:

#####
#   #
#   #
#   #
#####
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 #
有人能告诉我为什么在我的函数末尾有一个换行符吗?我试图找到一些答案,但每个主题都是关于添加换行符而不是删除换行符。
非常感谢您的帮助。

看起来您不想打印您调用的函数。函数本身调用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()

您的代码中有一些错误:

  • 您不需要从这些函数返回任何内容
  • 这些函数不需要调用print
  • 在这些情况下,不需要显式循环变量
  • 第一个函数中缺少一个
    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 #
输出:


行。

您的代码正在为我输出
#########
。。修复你的代码在从函数内部打印后,你也会返回它们。有什么原因吗?不要在使用相同loopvar名称的另一个循环中嵌套for循环。这导致了混乱。另外,
ligneEspace
中的最后一次打印没有
end='
您的代码被弄乱了。如果从函数本身调用
print
,则无需再次调用
print
。只需调用这些函数。另外,正如已经指出的,没有必要返回AK47所说的函数。您显示的代码不可能打印该输出。虽然这是朝着正确方向迈出的一步,但这并不是问题的实际解决方案,因为输出仍然混乱