[Python学习25] 关于函数更多的练习
在这一章的学习中,做了一些函数和变量的练习。并不是直接运行脚本,而是在脚本中定义了一些函数,把他们导入到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$ 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这位老兄解释的很清楚
具体错误提示如下:
-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_wordswords = 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_lastprint_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
转载于:https://blog.51cto.com/6150141/2086270
[Python学习25] 关于函数更多的练习相关推荐
- Python学习笔记12_函数
Python学习笔记12_函数 文章目录 Python学习笔记12_函数 1.函数定义 2.函数调用 3.函数的参数 3.1.可更改对象和不可更改对象参数 3.2.必需参数(位置参数) 3.3.关键字 ...
- Python学习之zip函数
Python 学习之 zip 函数 问题的引出 有时候,你可能想同时迭代两个序列.假设有下面两个列表: names = ['anne', 'beth', 'george', 'damon'] ages ...
- Python学习笔记:函数(Function)
Python学习笔记:函数(Function) 一.函数基本概念 函数是Python里组织与重用代码最重要的方法.一般来说,如果你期望多次重复相同或相似的代码,写一个可重用的函数可能是值得的.函数通过 ...
- [Python学习] 专题一.函数的基础知识
最近才开始学习Python语言,但就发现了它很多优势(如语言简洁.网络爬虫方面深有体会).我主要是通过<Python基础教程>和"51CTO学院 智普教育的pyt ...
- Python学习笔记——一些函数
本文对应头歌上的Python练习:https://www.educoder.net/paths/pn7qklv9 基础知识1: input( )函数 input()函数从控制台获得用户输入,无论用户在 ...
- Python学习:定义函数的默认参数和可变参数
一.默认参数 定义函数的时候,还可以有默认参数. 例如Python自带的 int() 函数,其实就有两个参数,我们既可以传一个参数,又可以传两个参数: >>> int('123') ...
- python常用函数-Python 学习:常用函数整理
整理Python中常用的函数 一,把字符串形式的list转换为list 使用ast模块中的literal_eval函数来实现,把字符串形式的list转换为Python的基础类型list from as ...
- Python学习:魔法函数
一.什么是魔法函数(网络用语) 以双下划线开始,双下滑线结尾.魔法函数是为了增强一个类的特性. 魔法函数可以随意定义某个类的特性,这些方法在进行特定的操作时会自动被调用. 1 需求:封装一个员工列表, ...
- 小甲鱼python003答案_小甲鱼:Python学习笔记003_函数
>>> # 函数 >>> def myFirstFunction(params1,params2...): print("这是我的第一个函数!" ...
最新文章
- 服务器-番外篇-搭建samba共享
- iOS 开发之几个 Demo 分享网站
- python的取负运算_python 负数取模运算实例
- 盘点实际项目应用中的最佳机器学习模型
- Yum介绍与常见用法
- javascript无提示关闭窗口,兼容IE,Firefox
- HDU 2159 完全背包
- 重磅!容器存储解决方案蓝皮书发布
- Python面向对象-0
- 分布式文件存储FastDFS之配置Nginx模块
- 更多数学趣题:走迷宫
- 高通camera结构(摄像头基础介绍)
- 第四范式蒋仁皓:什么才是构建企业AI的关键要素
- 【GIT】error: failed to push some refs to 'https://github.com/username/python.git'
- Windows打印管理解决方案
- 服! 买不起2.6亿一只的加密猫, 他用10分钟生了一窝!
- Java实现三角形图案绘制**
- Android 10动态申请读写权限
- Android安卓系统提示应用程序未安装的解决方法
- 使用Egret粒子编辑器实现烟雾效果
热门文章
- Redis实现计数器---接口防刷---升级版(Redis+Lua)
- 这个机器人不学数据集,“纯玩”get各类家务技能,LeCun觉得很赞
- 四年一度的菲尔兹奖揭晓,4位数学家折桂
- 干货分享 | 自然语言处理及词向量模型介绍(附PPT)
- npm安装失败,哪位大神帮忙看一下
- CentOS7.5安装Tigervnc-server
- PHP一阶段 html+css+js 练习题汇总
- PHP Web Shell in browser
- 传输层协议TCP和UDP
- CentOS6.x 下 LNMP环境搭建(二、安装 Nginx)