【问题标题】:getting rid of extra space with return statement [duplicate]用 return 语句去除多余的空间 [重复]
【发布时间】:2026-02-16 16:00:01
【问题描述】:

当我说有 12 个苹果时,句号之前有一个空格。有谁知道如何让空间不存在?

def main():
  apples = int(input("Enter the number of apples: "))
  time = float(input("Enter how much time in hours there is to find apples: "))
  print("There are", apples, "apples,", ENOUGH(apples), OBJECTIVE(time))
def ENOUGH(apples):
  if apples == 12:
    return("which is enough")
  elif apples < 12:
    return("which is not enough")
  elif apples > 12:
    return("which is too many")
def OBJECTIVE(time):
  if time >= 2:
    return TIME(time)
  elif time < 2:
    return(".")
def TIME(apples):
  if apples < 12:
    return(', but we have time to get more.')
  elif apples > 12:
    return(', but we have time to put some back.')
  elif apples == 12:
    return(".")
main()

【问题讨论】:

  • 你能说说实际输出和预期输出吗?

标签: python python-3.x


【解决方案1】:

使用字符串连接,比如

print("There are", apples, "apples,", ENOUGH(apples)+OBJECTIVE(time))

这会阻止分隔符(空格)出现在最后两个参数之间。

【讨论】: