【问题标题】:Nothing is printing out in the console, I thought I called it but I guess not and now I am stuck [closed]控制台中没有打印任何内容,我以为我调用了它,但我想没有,现在我被卡住了[关闭]
【发布时间】:2022-08-12 22:53:35
【问题描述】:
def animals(ani1, ani2, ani3):

 if ani1 == ani2 or ani1 == ani3:

     return \"Match was Found\"
 elif ani2 == ani1 or ani2 == ani3:

     return \"Match was Found\"
 elif ani3 == ani2 or ani3 == ani1:

     return \"Match was Found\"
 else:

     return \"No Match Found\"

 print(animals(\"dogs\", \"dogs\", \"cats\"))

  • 最后一行不应该缩进 - 这使它成为animals 函数的一部分,所以它永远不会被执行。
  • 您需要取消缩进您的 print(animals(... 行,否则它会在 animals 方法本身内部被调用
  • 无需包含您的代码图像,谢谢,文字更好。

标签: python


【解决方案1】:

这是因为您在函数中也缩进了 print(animals("dogs", "dogs", "cats")) 行,所以在调用函数之前它不会执行。

从最后一行删除缩进然后它会正常工作。

def animals(ani1, ani2, ani3):

 if ani1 == ani2 or ani1 == ani3:

     return "Match was Found"
 elif ani2 == ani1 or ani2 == ani3:

     return "Match was Found"
 elif ani3 == ani2 or ani3 == ani1:

     return "Match was Found"
 else:

     return "No Match Found"

print(animals("dogs", "dogs", "cats"))

【讨论】:

  • 值得补充的是,Python 通常缩进 4 个空格,因为 1 个空格的缩进差异非常微妙且容易被忽略。
【解决方案2】:

您的打印语句位于函数内部。将其移到函数之外(删除缩进),它将起作用。

【讨论】:

    猜你喜欢
    • 2015-11-12
    • 1970-01-01
    • 2019-08-31
    • 2018-04-22
    • 1970-01-01
    • 2016-04-07
    • 2011-02-15
    • 2017-01-09
    • 1970-01-01
    相关资源
    最近更新 更多