在终端中运行python脚本,没有打印或显示 - 为什么?

use*_*742 3 python terminal

通过艰难的方式学习Python,第25课.

我尝试执行脚本,结果如​​下:

myComp:lphw becca$ python l25 

myComp:lphw becca$ 
Run Code Online (Sandbox Code Playgroud)

终端中没有打印或显示任何内容.

这是代码.

def breaks_words(stuff): 
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words 

def sort_words(words):
    """Sorts the words."""
    return sorted(words)

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print word

def print_last_word(words):
    """Prints the last word after popping it off."""
    word = words.pop(-1)
    print word

def sort_sentence(sentence): 
"""Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)
Run Code Online (Sandbox Code Playgroud)

请帮忙!

wer*_*ika 12

您的所有代码都是函数定义,但您从不调用任何函数,因此代码不会执行任何操作.

使用def关键字just 定义函数,定义一个函数.它没有运行它.

例如,假设您在程序中只有这个功能:

def f(x):
    print x
Run Code Online (Sandbox Code Playgroud)

你告诉程序每当你打电话时f,你都希望它打印参数.但是你实际上并没有告诉你打电话f,而是在你打电话时该做什么.

如果你想在某个参数上调用该函数,你需要这样做,如下所示:

# defining the function f - won't print anything, since it's just a function definition
def f(x):
    print x
# and now calling the function on the argument "Hello!" - this should print "Hello!"
f("Hello!")
Run Code Online (Sandbox Code Playgroud)

因此,如果您希望程序打印某些内容,则需要对您定义的函数进行一些调用.什么调用和什么参数取决于你想要代码做什么!