Python AttributeError:';str';对象没有属性';流行音乐';在路上学东西
我有一个关于这个错误的持续问题,我试图解决这个问题,因为教程告诉我要修复这个代码。大多数代码都是拼写和数学错误,但是我无法解决这个AttributeError 教程网站: 以下是回溯错误:回溯(最近一次呼叫最后一次): 文件“C:\Python34\ex26.py”,第74行,在 打印第一个单词(句子)文件“C:\Python34\ex26.py”,第10行,打印第一个单词 word=words.pop(0) AttributeError:“str”对象没有属性“pop” 我的代码,作为测试,我必须修复教程代码:Python AttributeError:';str';对象没有属性';流行音乐';在路上学东西,python,string,list,attributes,attributeerror,Python,String,List,Attributes,Attributeerror,我有一个关于这个错误的持续问题,我试图解决这个问题,因为教程告诉我要修复这个代码。大多数代码都是拼写和数学错误,但是我无法解决这个AttributeError 教程网站: 以下是回溯错误:回溯(最近一次呼叫最后一次): 文件“C:\Python34\ex26.py”,第74行,在 打印第一个单词(句子)文件“C:\Python34\ex26.py”,第10行,打印第一个单词 word=words.pop(0) AttributeError:“str”对象没有属性“pop” 我的代码,作为测试,我
def break_words(stuff):
words = stuff.split(' ')
return words
def sort_words(words):
return sorted(words)
def print_first_word(words):
word = words.pop(0)
print(words)
def print_last_word(words):
word = words.pop(-1)
print(word)
def sort_sentence(sentence):
words = break_wrods(sentence)
return sort_words
def print_first_and_last(sentence):
words = break_words(sentence)
print_first_word(words)
print_last_word(words)
return words
def print_first_and_last_sorted(sentence):
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)
return words
print("Let's practice everything.")
print("You\'d need to know \' bout escapes with \\ that do \n newlines and \t tabs.")
poem = """
\tThe lovely world with logic
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explaination
\n\twhere there is none.
"""
print("-" * 10)
print(poem)
print("-" * 10)
five = 10 - 2 + 3 - 5
print("This should be five: %s " % five)
def secret_formula(started):
jelly_beans = started * 100
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
start_point = 10000
beans, jars, crates = secret_formula(start_point)
print("With a starting point of: %d " % start_point)
print("We'd have %d jeans, %d jars, and %d crates." % (beans,jars,crates))
start_point = start_point / 10
print("We can also do that this way: ")
print("We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point))
sentence = "All good\tthings come to those who wait."
words = sentence.split()
sorted_words = sort_words(sentence)
print_first_word(sentence)
print_last_word(sentence)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = sort_sentence(sentence)
print(sorted_words)
print_first_and_last(sentence)
print_first_and_last_sorted(sentence)
您正在此处传入一个字符串:
sentence = "All good\tthings come to those who wait."
# ...
print_first_word(sentence)
你要通过一个名单;大概你是想把单词
传进来;这是一个列表(调用str.split()
的结果):
使用以下命令:
print_first_word(words)
与此相反:
print_first_word(sentence)
谢谢,我无意中犯了一个错误,你的解释很完美。我是新来的堆栈溢出,所以很荣幸能解决这个小问题。
print_first_word(sentence)