错误跟踪:
C:\ Users \ Abhi.Abhi-PC \ Desktop \ PYE> ex25ex.py Traceback(最新版) 最后调用):文件“C:\ Users \ Abhi.Abhi-PC \ Desktop \ PYE \ ex25ex.py”,行 41,在print_last_word(句子)文件中 “C:\ Users \ Abhi.Abhi-PC \ Desktop \ PYE \ ex25ex.py”,第17行,in print_last_word word = words.pop(1)AttributeError:'str'对象 没有属性'pop'
这是代码
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)
sentence="island has lush vegetation, area. However, summers are cooler than those abundant."
break_words(sentence)
sort_words(sentence)
print_last_word(sentence)
print_last_word(sentence)
print_first_and_last_sorted(sentence)
print_first_and_last(sentence)
我无法解决问题的问题。我是python的新手。我正在从“如何学习python艰难的方式”这本书开始工作。
答案 0 :(得分:1)
您的代码存在的问题是您忽略了函数的返回值,尤其是break_words
,并始终对原始输入进行操作。这样可以解决问题:
words = break_words(sentence)
sort_words(words)
print_first_word(words)
print_last_word(words)
print_first_and_last_sorted(sentence)
print_first_and_last(sentence)
还有一个小问题,您应该使用words.pop(-1)
来访问单词列表中的最后一项。
您如何从错误消息中识别出问题。例外是一个表示'str' object has no attribute 'pop'
的AttributeError。显然,在行words.pop(0)
中,单词是一个字符串,而不是字符串列表,正如变量名所示。然后只需要快速浏览一下,看看为什么这个变量是一个字符串:当你用print_last_word(sentence)
调用函数时,你传递原始数据(类型为str
)而不是你可能的预处理数据本打算通过。
答案 1 :(得分:0)
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)
sentence="island has lush vegetation, area. However, summers are cooler than those abundant."
wordList=break_words(sentence)
print('')
print('wordList:',wordList)
print('')
print('Sorted :',sort_words(wordList))
print('')
print_first_word(wordList)
print('')
print_last_word(wordList)
print('')
print_first_and_last_sorted(sentence)
print('')
print_first_and_last(sentence)
<强> RESULT 强>
wordList: ['island', 'has', 'lush', 'vegetation,', 'area.', 'However,', 'summers', 'are', 'cooler', 'than', 'those', 'abundant.']
Sorted : ['However,', 'abundant.', 'are', 'area.', 'cooler', 'has', 'island', 'lush', 'summers', 'than', 'those', 'vegetation,']
island
abundant.
However,
vegetation,
island
abundant.
答案 2 :(得分:0)
您要在此处执行的操作是从字符串中删除单词。当您尝试sentence.pop()
时,您试图从不可变序列类型中删除元素。这意味着您正在尝试从字符串中删除元素,这没有多大意义。您应该做的是,将字符串分解为words.split(' ')
的单词,然后删除要从中删除的任何元素。因此,替换第17行的解决方案是:
word = words.split(' ').pop(len(words.split(' '))-1)
这将删除句子中的最后一个单词并将其分配给word
。
注意:这显然不是最有效的解决方案。我将其分解以使其可读。
希望这有帮助
答案 3 :(得分:0)
请参见pop()
不是字符串函数,因此可以创建一个函数,将n作为需要从字符串中弹出的字符的索引传递为str。
将以下代码写入弹出窗口:
def popout(str,n):
front = str[:n] # up to but not including n
back = str[n+1:] # n+1 through end of string
return front + back
根据我的理解,这可能会回答您的问题。如果没有,请 在评论部分纠正我。