2016-10-23 3 views
-1

У меня есть эта постоянная проблема с этой ошибкой, которую я пытался решить, поскольку учебник сказал мне исправить этот код. Большая часть кода - это орфографические и математические ошибки, однако я не могу решить этот атрибут AttributeError.Python AttributeError: объект 'str' не имеет атрибута 'pop' Learnpythonthehardway

Руководство сайта: https://learnpythonthehardway.org/book/exercise26.txt

Здесь ошибка отслеживающий: Traceback (самый последний вызов последнего):

File "C:\Python34\ex26.py", line 74, in print_first_word(sentence) File "C:\Python34\ex26.py", line 10, in print_first_word word = words.pop(0)

AttributeError: 'str' object has no attribute '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) 

ответ

0

Вы передаете строку здесь:

sentence = "All good\tthings come to those who wait." 
# ... 
print_first_word(sentence) 

Вы должны пройти в списке; по-видимому, вы хотели перевести в words; это список (результат str.split() вызова):

words = sentence.split() 
+0

Спасибо, я unknowly сделал ошибку, и ваши объяснения идеально. Im новый для переполнения стека, поэтому его честь решить эту небольшую проблему. –

0

Используйте это:

print_first_word(words) 

вместо этого:

print_first_word(sentence) 
Смежные вопросы