学习《Python基础教程(第二版)》笔记10--抽象3(练习)

来源:互联网 发布:清理软件残留 编辑:程序博客网 时间:2024/06/10 20:02

6.4.6 练习使用参数

1.

>>> def story(**kwds):  #处理关键字参数。

...     return 'once upon a time,there was a' '%(job)s called %(name)s.' %kwds

...

>>>

>>> story('nice','king')   #要加上关键字参数才行。

Traceback (most recent call last):

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

TypeError: story() takes exactly 0 arguments (2 given)

>>> story(job='nice',name='king')   #不加print返回的字符串加上 

'once upon a time,there was anice called king.'

>>> print story(job='nice',name='king')  #加上返回不加 

once upon a time,there was anice called king.

 

2.  #notepad++中编写代码不能调用.

>>> def interval(start, stop=None, step=1):

...     'imitates range() for step > 0'

...     if stop is None:

...             start,stop = 0, start

...     result = []

...     i = start

...     while i < stop:

...             result.append(i)  #list末尾添加新的对象。

...             i+=step

...     return result

...

>>>

>>> interval(10)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> interval(2,3,4)

[2]

>>> interval()

Traceback (most recent call last):

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

TypeError: interval() takes at least 1 argument (0 given)

>>> interval(0)

[]

0 0
原创粉丝点击