这是我遇到问题的代码:
def break_words(stuff):
"""This function will break up words."""
words = stuff.split(' ')
return words
def sort_words(words):
"""Sorts the words."""
return sorted(words)
def print_first_word(words):
def print_first_word(words):
"""Prints the first word after popping it off."""
word = raw_input('> ')
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
当我在终端中调用脚本时,我得到了
Traceback (most recent call last):
File "ex.py", line 87, in <module>
print_first_word(words)
File "ex.py", line 15, in print_first_word
word = words.pop(0)
AttributeError: 'str' object has no attribute 'pop'
我不明白这一点,因为在不同的文件中,我的代码基本相同,而且我在这里使用与完全相同的上下文中的pop。 (这是来自Learn Python The Hard Way练习的练习26)
答案 0 :(得分:0)
一切都是正确的,因为字符串没有方法弹出,但您可以将print_first_word转换为列表。