首页 > 编程语言 > 详细

Python学习25:关于函数更多的练习

时间:2018-03-13 23:41:09      阅读:347      评论:0      收藏:0      [点我收藏+]
在这一章的学习中,做了一些函数和变量的练习。并不是直接运行脚本,而是在脚本中定义了一些函数,把他们导入到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)等。
具体错误提示如下:

-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提示名称未定义。

>>> 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目录。

-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

Python学习25:关于函数更多的练习

原文:http://blog.51cto.com/6150141/2086270

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!