Hàm trong python

mình đang học cuốn “Learn Python The Hard Ways” cho mình hỏi đoạn code nay o Exercise 25:

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)

Minh run theo huong dan cua anh Dat va sach:

$ python
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>import ex25
sentence = "All good things come to those who wait."
words = ex25.break_words(sentence)
Traceback (most recent call last):
         File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'break_word'

Ma no cu loi hoai ai cho minh y kien khac phuc loi voi! Cam on!

break_word khác so với break_words

1 Like

Cho mình hỏi xíu vẫn cái ở trên sao khi:

$ python
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.

import ex25
sentence = “All good things come to those who wait.”
words = ex25.break_words(sentence)
print words
[‘All’, ‘good’, ‘things’, ‘come’, ‘to’, ‘those’, ‘who’, ‘wait.’]

sorted_words=ex25.sort_words(words)
print sorted_words
[‘All’, ‘come’, ‘good’, ‘things’, ‘those’, ‘to’, ‘wait.’, ‘who’]

ex25.print_first_word(words)
Khúc này lỗi
Mà sao nó cứ báo lỗi ở line 12 dậy! Chỉ dùm mình. Cảm ơn!

Bạn cho mình xem lỗi được không?

loi hoi nay minh sua duoc roi cam mon, ban cho minh hoi:

import ex25
sentence = “All good things come to those who wait.”
words = ex25.break_words(sentence)
words
[‘All’, ‘good’, ‘things’, ‘come’, ‘to’, ‘those’, ‘who’, ‘wait.’]

sorted_words = ex25.sort_words(words)
sorted_words
[‘All’, ‘come’, ‘good’, ‘things’, ‘those’, ‘to’, ‘wait.’, ‘who’]

ex25.print_first_word(words)
All

ex25.print_last_word(words)
wait.
Cai dong ex25.print_last_word(words) thay vi chay ra chu “wait” thi minh lai ra:
[‘good’, ‘things’, ‘come’, ‘to’, ‘those’, ‘who’]
giai thich cho minh hieu voi! Cam mon!

Để mình nói về việc import này trong Python một cách đơn giản nhất
Ví dụ bạn có một file a.py

def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words

split là một phương thức giúp bạn tách các phần tử ra bằng các chuỗi Ví dụ

>>> name = 'An_Thuy'
>>> name.split('_')
['An', 'Thuy']
>>> name    # không bị thay đổi
'An_Thuy'

Thế nên trong ví dụ của bạn hàm break_words là tách chữ nó sẽ

>>> stuff = 'All good things come to those who wait'
>>> words = stuff.split(' ')
>>> words
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait']

Sau đó return về thì words = ex25.break_words(sentence) tương đương với
words = ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait']
Về sorted thì nó sẽ sắp xếp các chữ cái theo thứ tự ‘abc’

>>> lst = ['b', 'c', 'a']
>>> sorted(lst)
['a', 'b', 'c']

return về thì sorted_words = ex25.sort_words(words) thì sorted_words sẽ nhận được một dãy sắp xếp như bạn tháay
print_first_word là in chữ đầu tiên mà chữ đầu tiên là phần tử đầu tiên trong mảng. Phần tử đầu tiên trong mảng là [0]. Let’s take a look at function print_first_word

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print word

Giải thích về pop.

>>> lst = [1, 2, 3] # vị trí 0 là số 1, vị trí 1 là số 2, vị trí 2 là số 3
>>> lst.pop(0) # nếu là 0 thì có thể viết ngắn là lst.pop() thôi cũng được
1
>>> lst # lst có thay đổi
[2, 3]

Thế nên word = words.pop(0) thì word sẽ nhận cái chữ được pop ra ở vị trí 0. print_last_word hoạt động tương tự chỉ khác vị trí là -1 là vị trí cuối cùng để nói sơ qua về vị trí

['a', 'b', 'c']
  0    1    2   đây là một kiểu vị trí
 -3   -2   -1   đây cũng là một kiểu

:smile: mong là bạn hiểu

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?