Python LPTHW EX25,我认为有';我的powershell有什么问题吗?即使复制和粘贴
所以将其复制到一个名为ex25.py的文件中-Python LPTHW EX25,我认为有';我的powershell有什么问题吗?即使复制和粘贴,python,Python,所以将其复制到一个名为ex25.py的文件中- 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)
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 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)
现在在Blueshell中,将cd复制到文件夹所在的位置,键入“Python”激活Python,然后从Zed的网站复制/粘贴此代码-
import ex25
sentence = "All good things come to those who wait."
words = ex25.break_words(sentence)
words
sorted_words = ex25.sort_words(words)
sorted_words
ex25.print_first_word(words)
ex25.print_last_word(words)
words
ex25.print_first_word(sorted_words)
ex25.print_last_word(sorted_words)
sorted_words
sorted_words = ex25.sort_sentence(sentence)
sorted_words
ex25.print_first_and_last(sentence)
ex25.print_first_and_last_sorted(sentence)
结果(我的问题)-
导入ex25
>>>一切美好的事物都会降临到等待的人身上
>>>单词=ex25。断开单词(句子)
>>>言语
[“所有”、“好”、“事物”、“来”、“到”、“那些”、“谁”、“等等”。]
>>>排序单词=ex25。排序单词(单词)
>>>分类词
[“所有”、“来”、“好”、“事情”、“那些”、“去”、“等”、“谁”]
>>>ex25.打印第一个单词(单词)
全部的
>>>ex25.打印最后一个单词(单词)
等待
>>>言语
[‘好’、‘东西’、‘来’、‘到’、‘那些’、‘谁’]
>>>ex25.打印第一个单词(已排序的单词)
全部的
>>>ex25.打印最后一个单词(已排序的单词)
谁
>>>分类词
[‘来’、‘好’、‘事情’、‘那些’、‘去’、‘等等’。]
>>>排序的单词=ex25。排序的句子(句子)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“模块”对象没有“排序句子”属性
>>>分类词
[“所有”、“来”、“好”、“事情”、“那些”、“去”、“等”、“谁”]
>>>ex25.打印第一个和最后一个(句子)
全部的
等待
>>>ex25.打印排序后的第一个和最后一个(句子)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“ex25.py”,第33行,在打印中\u first\u和\u last\u排序
单词=排序句子(句子)
NameError:未定义全局名称“sort\u句”
>>>
如果有人能帮忙,我将不胜感激!在这上面浪费了过去的40分钟-uu-尝试dir(ex25)
浏览模块,并确保已排序的单词
仅在返回的单词中,没有有趣的额外字符,等等。。。
>>> import ex25
>>> sentence = "All good things come to those who wait."
>>> words = ex25.break_words(sentence)
>>> words
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
>>> sorted_words = ex25.sort_words(words)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> ex25.print_first_word(words)
All
>>> ex25.print_last_word(words)
wait.
>>> words
['good', 'things', 'come', 'to', 'those', 'who']
>>> ex25.print_first_word(sorted_words)
All
>>> ex25.print_last_word(sorted_words)
who
>>> sorted_words
['come', 'good', 'things', 'those', 'to', 'wait.']
>>> sorted_words = ex25.sort_sentence(sentence)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'sort_sentence'
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> ex25.print_first_and_last(sentence)
All
wait.
>>> ex25.print_first_and_last_sorted(sentence)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ex25.py", line 33, in print_first_and_last_sorted
words = sort_sentence(sentence)
NameError: global name 'sort_sentence' is not defined
>>>