本博客列出的答案不是来自官方资源,是我自己做的练习,如果有疑问或者错误,欢迎讨论。

11-1.
参数。比较下面3个函数:
def countToFour1():
    for eachNum in range(5):
        print eachNum

def countToFour2(n):
    for eachNum in range(n, 5):
        print eachNum

def countToFour3(n=1):
    for eachNum in range(n, 5):
        print eachNum

给定如下的输入直到程序输出,你认为会发生什么?向下表11.2填入输出。如果你认为给定的输入会发生错误的话填入“Error”或者如果没有输出的话填入“NONE”。
【注】题目里面提到的表11.2,就是本页题11-2下面的那个表。也就是要看分别输入值2,4,5和nothing到这三个函数,会有什么结果。
【答案】
代码如下:

>>> def countToFour1():
...     for eachNum in range(5):
...             print eachNum
...
>>> def countToFour2(n):
...     for eachNum in range(n, 5):
...             print eachNum
...
>>> def countToFour3(n = 1):
...     for eachNum in range(n, 5):
...             print eachNum
...

>>> countToFour1(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: countToFour1() takes no arguments (1 given)
>>> countToFour2(2)
2
3
4
>>> countToFour3(2)
2
3
4

>>> countToFour1(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: countToFour1() takes no arguments (1 given)
>>> countToFour2(4)
4
>>> countToFour3(4)
4

>>> countToFour1(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: countToFour1() takes no arguments (1 given)
>>> countToFour2(5)
>>> countToFour3(5)

>>> countToFour1()
0
1
2
3
4
>>> countToFour2()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: countToFour2() takes exactly 1 argument (0 given)
>>> countToFour3()
1
2
3
4

 

11-2.
函数。创建一个函数,返回两个数字的和以及乘积。
【注】根据英文版修改了题目。
【答案】
代码如下:

def func(a, b):
    c = a * b
    d = a + b
    return c, d

a = raw_input("Please input the first number: ...  ")
# From www.cnblogs.com/balian/
b = raw_input("Please input the second number: ...  ")
print Multi_P(float(a), float(b))

 

11-3.
函数。在这个练习中,我们将实现max()和min()内建函数。
(a)写分别带两个元素返回一个较大和较小元素,简单的max2()和min2()函数。他们应该可以用任意的Python对象运作。举例来说,max2(4,8)和min2(4,8)会各自每次返回8和4。
(b)创建使用了在a部分中的解来重构max()和min()的新函数my_max()和my_min()。这些函数分别返回非空队列中一个最大和最小值。他们也能带一个参数集合作为输入。用数字和字符串来测试你的解。
【答案】
(a)代码如下:

def max2(a, b):
	if a >= b:
		return a
	else:
		return b

def min2(a, b):
	if a >= b:
		return b
	else:
		return a


(b)代码如下:

def max2(a, b):
    if a >= b:
        return a
    else:
        return b

def min2(a, b):
    if a >= b:
        return b
    else:
        return a
    
def my_max(*List):
    Max = List[0]
    for eachItem in List:
        Max = max2(eachItem, Max)
    return Max

def my_min(*List):
    Min = List[0]
    for eachItem in List:
        Min = min2(eachItem, Min)
    return Min

print my_max(3,6,9,2,8)
print my_min(3,6,9,2,8)

 

【未完】函数max()和min()能处理参数为一个列表的情况,比如,max([1,2,3])的结果是1。而这里的函数my_max([1,2,3])的结果是[1,2,3]。程序还需要改进。

 

11-4.
返回值。给你在5-13的解创建一个补充函数。创建一个以分为单位的总时间,以及返回一个以小时和分为单位的等价的总时间。
【答案】
代码如下:

def conversion(a, b):
    return a * 60 + b

time = raw_input('Please input the time in HH:MM format: ... ')
print time, 'equals to'
t = time.split(':')
print conversion(int(t[0]), int(t[1])), 'minutes'
# From www.cnblogs.com/balian/

 

11-5.
默认参数。更新你在练习5-7中创建的销售税脚本以便让销售税率不再是函数的必要参数。使用你的地方税率作为默认参数。
【答案】
代码如下:

def tax(pureprice, rate = 0.13):
    return pureprice * rate


purePrice = float(raw_input('Please input the price: ... '))
print 'You should pay:'
print 'Subtotal:  %10.2f ' % purePrice
print 'Sales Tax: %10.2f ' % tax(purePrice)
# From www.cnblogs.com/balian/

 

11-6.
变长参数。写一个称为printf()的函数。有一个值参数,格式字符串。剩下的就是根据格式化字符串的值,要显示在标准输出上的可变参数,格式化字符串中的值允许特别的字符串格式操作指示符,如%d,%f,etc。提示:解是很琐碎的--无需实现字符串操作符功能性,但你需要显示用字符串格式化操作(%)。
【注】附英文版题目的原文:
Variable-Length Arguments. Write a function called printf(). There is one positional argument, a format string. The rest are variable arguments that need to be displayed to standard output based on the values in the format string, which allows the special string format operator directives such as %d, %f, etc. Hint: The solution is trivial—there is no need to implement the string operator functionality, but you do need to use the string format operator (%) explicitly.
【答案】
代码如下:

>>> def printf(fmt, *args):
...     print fmt % args
...

 

【执行结果】
>>> printf('%s', 'tom')
tom
>>> printf('%d', 12)
12
>>> printf('%f', 12)
12.000000
>>>
【参考】
做这题我参考了下面链接:
http://www.velocityreviews.com/forums/t676359-emulate-a-printf-c-statement-in-python.html

posted on 2012-07-30 08:43  balian  阅读(1694)  评论(0编辑  收藏  举报