我正在阅读电子书“以艰难的方式学习python”在示例25中,我将所有内容编码正确但是当我尝试在powershell中加载文件时,没有任何内容出现。没有错误或任何东西,但文件剂量加载。它还说在你发现所有错误之后在python中加载它,但它也在那里加载。我的错误可能是什么?为什么不加载?
这是我的代码:
def break_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 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)
答案 0 :(得分:1)
该文件已导入示例中的shell并运行如下,重要的是从python shell
而不是powershell
运行:
>>> import ex25 # import file
>>> sentence = "All good things come to those who wait." # create variable sentence
>>> words = ex25.break_words(sentence) # create variable words
>>> words # input
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.'] # output
>>> sorted_words = ex25.sort_words(words) # create `sorted_words` variable using `sort_words` method
>>> sorted_words #
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who'] # output
>>> ex25.print_first_word(words) # call `print_first_word` method
All # output
>>> ex25.print_last_word(words) # call `print_last_word` method
wait. # output
在保存py文件的同一目录中打开一个shell并运行上面的命令,
不要输入>>>
,这只是示例
如果您想在使用python ex25.py
if __name__== "__main__"
的PowerShell运行脚本时输出:
def break_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 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)
if __name__=="__main__":
sentence = "All good things come to those who wait." # create variable sentence
words = break_words(sentence) # create variable words
print words # input
sorted_words = sort_words(words) # create `sorted_words` variable using `sort_words` method
print sorted_words #
print_first_word(words) # call `print_first_word` method
print_last_word(words) # call `print_last_word` method
答案 1 :(得分:0)
您已在文件中定义了一堆函数。你期望看到什么输出?
如果您想查看任何内容,则需要执行其中一个过程。例如......
break_words('foo bar')
答案 2 :(得分:0)
在书中找一个主要的'与功能一起使用的程序。它可能在另一页左右。您可能只加载了这些函数。