Python 为什么下面的代码没有输出?

Python 为什么下面的代码没有输出?,python,python-3.4,Python,Python 3.4,你需要调用这个函数 加 到代码末尾。您已经声明了一个函数,在其中做了一个print语句,但您从未实际调用过该函数。要调用它,只需在程序中键入它的名称 重要的是,请确保调用声明下面的函数,因为它是过程性的解释器无法知道displayIntro是您希望它调用的方法。想象一下,可能有10种不同的顶级方法。最简单的修复方法是只调用主作用域中的方法: displayIntro() 解决办法很简单。 您已经定义了该函数,但从未调用过该函数。。 只有在调用该函数时,才会执行该函数。。 代码如下所示: imp

你需要调用这个函数


到代码末尾。

您已经声明了一个函数,在其中做了一个print语句,但您从未实际调用过该函数。要调用它,只需在程序中键入它的名称


重要的是,请确保调用声明下面的函数,因为它是过程性的

解释器无法知道displayIntro是您希望它调用的方法。想象一下,可能有10种不同的顶级方法。最简单的修复方法是只调用主作用域中的方法:

displayIntro()
解决办法很简单。 您已经定义了该函数,但从未调用过该函数。。 只有在调用该函数时,才会执行该函数。。 代码如下所示:

import random
import time
def displayIntro():
    print('You are in a land full of dragons. You see a cave in front of you. In one cave, there is a dragon who will share his treasure with you. The other cave contains a dragon that will eat you on sight.')

displayIntro()

您将能够获得所需的输出。

因为您从未调用displayIntro方法。你需要在DisplayIntro中添加一行没有缩进的新行,我不知道为什么人们会对此投反对票。。。新用户,python新手,给他一个如何提问的提示questions@L_Church人们并没有对用户投反对票,而是在估计这个问题对未来的读者有多有用。这看起来不是很有用——这是一个微不足道的疏忽。当然,它可能会发生在每个人身上,但可能会发生在数百万个不同的原因上,并且通过遵循Python教程的前几个示例之一很容易修复。它与这个特定脚本中的编程风格无关,它是过程性的。作为一个noob,您仍然需要记住这一点。当然,你不会每天都看到它,但很高兴记住,你不能在上面打电话是的,但这与程序无关。
import random
import time
def displayIntro():
    print('You are in a land full of dragons. You see a cave in front of you. In one cave, there is a dragon who will share his treasure with you. The other cave contains a dragon that will eat you on sight.')

displayIntro()
import random
import time

def displayIntro():
    print('You are in a land full of dragons. You see a cave in front of you. In 
           one cave, there is a dragon who will share his treasure with you. The 
           other cave contains a dragon that will eat you on sight.')

displayIntro()