Fehler Spur:Attribute: 'str' Objekt hat kein Attribut 'pop'
C:\Users\Abhi.Abhi-PC\Desktop\PYE>ex25ex.py Traceback (most recent call last): File "C:\Users\Abhi.Abhi-PC\Desktop\PYE\ex25ex.py", line 41, in print_last_word(sentence) File "C:\Users\Abhi.Abhi-PC\Desktop\PYE\ex25ex.py", line 17, in print_last_word word = words.pop(1) AttributeError: 'str' object has no attribute 'pop'
Hier ist der Code
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)
Ich bin nicht in der Lage, das Problem mit dem Problem Figur. Ich bin neu in Python. Ich arbeite nach dem Buch "Wie man Python hart lernt".
wie der Fehler sagt, Sie versuchen, 'pop()' auf der 'str' zu nennen, aber diese Funktion ist nicht mit Stichen –
Satz ist eine Zeichenfolge, aber in' print_last_word 'könnten Sie erwarten, dass es dort zu sein die Funktion zu arbeiten ... auch für das letzte Wort möchten Sie vielleicht 'words.pop (-1)' statt 1 machen. – Suparshva
was soll ich tun, um Pop zu benutzen? –