为什么以下代码没有输出?

时间:2018-01-18 16:18:01

标签: python python-3.4

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.')

4 个答案:

答案 0 :(得分:2)

你需要调用函数

添加

displayIntro()

到代码的末尾。

答案 1 :(得分:1)

您已经声明了一个函数,在其中创建了一个print语句,但您实际上从未调用过该函数。所有你要做的就是在程序中输入它的名称。

重要的是,请务必调用声明属于声明,因为它是程序性的

答案 2 :(得分:1)

解释器无法知道displayIntro是您希望它调用的方法。想象一下,可能有10种不同的顶级方法。最简单的解决方法是在主范围内调用方法:

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()

答案 3 :(得分:1)

解决方案很简单。 你已经定义了这个函数,但是你从未调用过函数.. 只有在调用该函数时,才会执行该函数。 所以代码看起来像这样:

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()

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