[Python學習25] 關於函數更多的練習

在這一章的學習中,做了一些函數和變量的練習。並不是直接運行腳本,而是在腳本中定義了一些函數,把他們導入到Python中通過執行函數的方式運行。先看代碼:

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)

可以看到這個程序中只定義了函數,並沒有調用函數並打印出來。我們需要使用import的方法把整個程序導入到python中,然後直接在python中使用程序中的各種功能。
導入函數的方法有兩種:import no25 或 from no25 import * (我寫的腳本名稱叫no25.py)
下面是執行結果:

-userdeMacBook-Air:desktop user$ python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 12:54:16) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import no25
>>> sentence = "I am the king of the world!"
>>> word = no25.break_words(sentence)
>>> word
['I', 'am', 'the', 'king', 'of', 'the', 'world!']
>>> sorted_word = no25.sort_words(word)
>>> sorted_word
['I', 'am', 'king', 'of', 'the', 'the', 'world!']
>>> no25.print_first_word(word)
I
>>> no25.print_last_word(word)
world!
['am', 'the', 'king', 'of', 'the']
>>> no25.print_first_word(word)
am
>>> no25.print_last_word(word)
the
>>> sorted_sentence = no25.sort_sentence(sentence)
>>> sorted_sentence
['I', 'am', 'king', 'of', 'the', 'the', 'world!']
>>> no25.print_first_and_last(sentence)
I
world!
>>> no25.print_first_and_last_sorted(sentence)
I
world!
>>> 
-userdeMacBook-Air:Desktop user$ python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 12:54:16) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from no25 import *
>>> sentence = "All things was very good!"
>>> short = break_words(sentence)
>>> short
['All', 'things', 'was', 'very', 'good!']
>>> sort_words(short)
['All', 'good!', 'things', 'very', 'was']
>>> print_first_word(short)
All
>>> print_last_word(short)
good!
>>> sorted_sentence = sort_sentence(sentence)
>>> short
['things', 'was', 'very']
>>> sorted_words = sort_sentence(sentence)
>>> sorted_words
['All', 'good!', 'things', 'very', 'was']
>>> print_first_and_last(sentence)
All
good!
>>> print_first_and_last_sorted(sentence)
All
was
>>> 

下面是在Python中執行時遇到的一些錯誤:

錯誤1:split方法中引號裏沒有添加空格。
'split'方法中必須指定一個分隔符,如果引號中沒有任何內容,就會提示“語法錯誤”,"ValueError: empty separator"。
正確用法:使用split(' '),分隔符,默認爲所有的空字符,包括空格、換行(\n)、製表符(\t)等。

Bing到這個網頁,在StackOverflow上有人有同樣的問題。
https://stackoverflow.com/questions/20826788/str-split-giving-me-valueerror-empty-separator-for-a-sentence-in-the-for

可惜我的網絡一直打不開網頁,只能通過Bing的Cache來訪問,555……
http://cncc.bingj.com/cache.aspx?q=ValueError%3a+empty+separator&d=5000365845973185&mkt=en-US&setlang=en-US&w=WwhQN8t8C5Lh52BiYEvwWPh7GKnyWyYu

Eldad AK這位老兄解釋的很清楚
[Python學習25] 關於函數更多的練習
具體錯誤提示如下:

-userdeMacBook-Air:desktop user$ python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 12:54:16) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import sys
>>> import no25
>>> s = "There are many books."
>>> sen = no25.break_words(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "no25.py", line 4, in break_words
    words = stuff.split('')
 ValueError: empty separator

錯誤2:調用函數打錯字導致python提示名稱未定義。
我在程序中第46行下面調用了一個函數print_first_words(),但是在程序裏並沒有定義這個函數,而是有print_first_word()這個函數,所以是手誤打錯了,python的錯誤提示"NameError: …… is not defined",可以幫助我們快速定位問題。

>>> no25.print_first_and_last(sentence)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "no25.py", line 46, in print_first_and_last
   print_first_words(words)
NameError: global name 'print_first_words' is not defined

錯誤3:當前目錄沒有no25.py腳本,我的腳本放在Desktop下,而新開的mac Command Line的目錄爲當前用戶的Home目錄。
可以看到python提示"No module named no25",說明python在庫中找不到叫no25的模塊,仔細觀察一下,發現我使用的是相對路徑,當前目錄是~,也就是user用戶的家目錄,所以找不到。

-userdeMacBook-Air:~ user$ python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 12:54:16) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from no25 import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named no25
>>> import no25
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named no25
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章