2016-10-23 3 views
-1

자습서에서이 코드를 수정 해 주겠다고 말한대로이 오류를 해결하려고했습니다. 대부분의 코드는 철자법과 수학 오류이지만이 AttributeError는 해결할 수 없습니다.Python AttributeError : 'str'객체에 'pop'속성이 없습니다. Learnpythonthehardway

튜토리얼 사이트 : 여기 https://learnpythonthehardway.org/book/exercise26.txt

는 역 추적 오류입니다 : 역 추적 (마지막으로 가장 최근 통화) :

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 대신에 전달하려고했다.

words = sentence.split() 
+0

감사합니다, 나는 unknowly 실수를하고 explaination 그 목록합니다 (str.split() 호출의 결과)입니다 완벽합니다. 새로운 스택 오버플로 그래서이 작은 문제를 해결할 수있는 영광입니다. –

0

사용이 : 대신의

print_first_word(words) 

:

print_first_word(sentence) 
관련 문제