博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Python学习25] 关于函数更多的练习
阅读量:7128 次
发布时间:2019-06-28

本文共 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这位老兄解释的很清楚

[Python学习25] 关于函数更多的练习
具体错误提示如下:

-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

你可能感兴趣的文章
软件开发人员应具备的基本素质 !!!
查看>>
无线运维——J2ME和WAP运维方式的优缺点
查看>>
生产环境Shell脚本Ping监控主机是否存活(多种方法)
查看>>
关于SQLServer2000中触发器的使用——多行数据提交
查看>>
commons-fileupload 1.3.1 上传测试
查看>>
红帽集群套件RHCS四部曲(概念篇)
查看>>
TFS配置(二)
查看>>
GeoServer地图开发解决方案(五):基于Silverlight技术的地图客户端实现
查看>>
Android应用程序键盘(Keyboard)消息处理机制分析(3)
查看>>
Linux上连接Microsoft SQL Server 2005
查看>>
私有云管理-Windows Azure Pack
查看>>
Linux下文件和目录的颜色代表的含义
查看>>
Forefront Client Security服务器部署
查看>>
Crystal Reports中的字段
查看>>
一个例子探究jQuery的Ajax应用(二)
查看>>
PPT of "SharePoint 2007 网站性能优化"
查看>>
爪哇国新游记之三十四----Dom4j的XPath操作
查看>>
node17
查看>>
Java程序性能优化4
查看>>
第一次负责项目总结
查看>>