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

当我说有 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))

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

【讨论】:

    猜你喜欢
    • 2016-01-29
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    相关资源
    最近更新 更多