本文共 4747 字,大约阅读时间需要 15 分钟。
在这一章的学习中,做了一些函数和变量的练习。并不是直接运行脚本,而是在脚本中定义了一些函数,把他们导入到Python中通过执行函数的方式运行。先看代码:
def break_words(stuff): """This function will break up words for us.""" words = stuff.split(' ') return wordsdef 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 worddef print_last_word(words): """Prints the last word after popping it off.""" word = words.pop(-1) print worddef 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$ pythonPython 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 12:54:16) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType "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)Iworld!>>> no25.print_first_and_last_sorted(sentence)Iworld!>>>
-userdeMacBook-Air:Desktop user$ pythonPython 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 12:54:16) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType "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)Allgood!>>> print_first_and_last_sorted(sentence)Allwas>>>
下面是在Python中执行时遇到的一些错误:
错误1:split方法中引号里没有添加空格。
'split'方法中必须指定一个分隔符,如果引号中没有任何内容,就会提示“语法错误”,"ValueError: empty separator"。正确用法:使用split(' '),分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。Bing到这个网页,在StackOverflow上有人有同样的问题。
可惜我的网络一直打不开网页,只能通过Bing的Cache来访问,555……
Eldad AK这位老兄解释的很清楚
-userdeMacBook-Air:desktop user$ pythonPython 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 12:54:16) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType "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 "", line 1, in 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 "", line 1, in 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$ pythonPython 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 12:54:16) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> from no25 import *Traceback (most recent call last): File "", line 1, in ImportError: No module named no25>>> import no25Traceback (most recent call last): File " ", line 1, in ImportError: No module named no25
转载于:https://blog.51cto.com/6150141/2086270