python license函数_[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)等。

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 "", 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$ 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 "", line 1, in

ImportError: No module named no25

>>> import no25

Traceback (most recent call last):

File "", line 1, in

ImportError: No module named no25

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值