本办法学python习题25 更多更多的实践

这个习题与前面的函数习题不同,前面的习题通过terminal可以直接运行,而这个习题是将.py作为模块导入到Python里面,并自己运行程序中的函数。

代码如下:

def break_words(stuff):
	"""This funciton 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):
	"""Print the first word after popping it off"""
	word = words.pop(0)
	print word
	
def print_last_word(words):
	"""Print the last word after poping it off."""
	word = words.pop(-1)
	print word
	
def sort_sentence(sentence):
	"""Takes in a ful sentence and returns the sorted words."""
	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)
代码中的函数(split/sorted/pop)以及字典元素的排序,可以通过网上的资料查到,下次博客再写。这里具体想记录一下程序导入到python内以及自己运行的过程:

1,检查代码的准确性:

在终端Terminal中输入

mystuff simengred$ python ex25.py
Terminal中没有报错,说明代码部分没有异常。接下来将程序以模块导入到Python内。

2,导入,输入以下代码:

mystuff simengred$ python
反馈信息如下:
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 12:01:12) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import ex25

这里就已经完成了ex25模块的导入,在import的时候不需要加.py后缀,在这个模块里定义的函数也可以直接调用出来。

3,执行代码第一个功能:break_words函数。函数的功能:将字符串赋值给sentence,然后用break_words函数对字符串进行切片并返回words(当然函数里面的words是一个临时变量)返回的word是一个列表

>>> sentence = "All good things come to those who wait."

>>> words = ex25.break_words(sentence)

>>> words

['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']

4,执行代码的第二个功能:对列表进行排序

>>> sorted_words = ex25.sort_words(words)

>>> sorted_words

['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']

特别注意,在使用函数时记得不要漏写ex25.这个部分。

5,执行代码的第三个功能:打印列表的第一个元素。

>>> ex25.print_first_word(words)

All

6,执行代码的第四个功能:打印列表的最后一个元素

>>> ex25.print_last_word(words)

wait.


7,执行代码第五个功能:pop函数的补充说明

>>> words

['good', 'things', 'come', 'to', 'those', 'who']

8,打印sorte_word的第一个、最后一个元素

>>> ex25.print_first_word(sorted_words)

All

>>> ex25.print_last_word(sorted_words)

who

>>> sort_words

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

NameError: name 'sort_words' is not defined

>>> sorted_words

['come', 'good', 'things', 'those', 'to', 'wait.']


9,看来sorted_words在经历第一个和最后一个元素打印的pop函数已经缺少2个元素。接下来使用sort_sentence(注意不是sort_word)函数,重新对sentence进行切片和排序。

>>> sort_words = ex25.sort_sentence(sentence)

>>> sort_words

['All', 'come', 'things', 'those', 'to', 'wait.', 'who']

>>> 

10,最后一个功能打印排序后的第一个和最后一个元素

>>> ex25.print_first_and_last_sorted(sentence)

All

who

>>> 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值