【问题标题】:Returning None value without me asking for it [duplicate]在没有我要求的情况下返回 None 值[重复]
【发布时间】:2021-06-27 20:23:19
【问题描述】:
def city_country(city, country):
    message = print(f"{city} is in the {country}! ")
    return message

city_n = input("What is the city called? ")
country_n = input("Where is it located? ")

final_message = city_country(city_n , country_n)
print(final_message)

它不应该给我 None 。我做错了吗?

【问题讨论】:

  • 第2行:变量message是函数print的返回值。但是print 会返回None,所以你会得到None
  • print 函数没有返回任何东西,所以你得到的是None
  • 你想要input() 函数,而不是print() 函数:)
  • @khuynh 很好,它返回None
  • @juanpa.arrivillaga 隐式无 d:

标签: python


【解决方案1】:

您不能将变量定义为打印函数,因为 print 不返回任何内容,它只是打印一些内容

【讨论】:

  • 打印函数返回None。在 Python 中,所有没有显式返回的函数都将返回 None
  • 当然可以print 总是返回 None,所以它不是很有用
猜你喜欢
  • 2017-06-26
  • 2014-04-09
  • 2019-01-31
  • 1970-01-01
  • 2020-06-20
  • 2020-11-04
  • 2019-09-07
  • 2013-05-29
  • 2018-09-15
相关资源
最近更新 更多