Exercises 18
代码
# this one is like your scripts with argvdef print_two(*args): arg1, arg2 = args print "arg1: %r, arg2: %r" % (arg1,arg2) # OK, that *args is actually pointless, we can just do thisdef print_two_again(arg1,arg2): print "arg1: %r, arg2: %r" % (arg1,arg2) # this just takes one argumentdef print_one(arg1): print "arg1: %r" % arg1 # this one takes no argumentsdef print_none(): print "I got nothin'."print_two("Jer","Chou")print_two_again("Jer","Chou")print_one("First!")print_none()
输出
Notes:
①注意函数的定义命令def及冒号。函数命名方法类似变量命名,名称可以随便取,但最好函数名称能够体现函数功能。
②圆括号"()"中的是函数的参数,函数可以接受0个、1个或多个参数,可以通过解包的方式给出,也可以直接给出,后者更为简便。
③函数名称和参数确定以后,用冒号结束本行,开始下一行缩进。缩进一般使用4个空格,缩进的语句构成函数主体。
④调用函数时,适用函数的名称并给定相应数量的参数
Exercise 19
代码
def cheese_and_crackers(cheese_count, boxes_of_crackers): print "You have %d cheeses!" % cheese_count print "You have %d boxes of crackers!" % boxes_of_crackers print "Man that's enough for a party!" print "Get a blanket. \n" print "We can just give the function numbers directly:"cheese_and_crackers(20, 30)print "OR, we can use variables from our script:"amount_of_cheeses = 10amount_of_crackers = 50cheese_and_crackers(amount_of_cheeses, amount_of_crackers)print "We can even do math inside too:"cheese_and_crackers(10 + 20, 5 + 6)print "And we can combine the two, vareables and math:"cheese_and_crackers(amount_of_cheeses + 100, amount_of_crackers + 1000)
输出
Notes:
①局部变量和全局变量的区别。函数内定义的变量无特殊声明时均为局部变量,对函数体外的变量赋值无影响。除非必要,尽量避免定义相同的全局变量名称和函数变量名称。
②可以直接给定函数的参数,可以通过数学运算,可以通过变量,也可以通过其他函数的返回结果作为函数的参数。
③加分习题
def print_whatever(arguement): print arguement, "\n"#运行函数的不同方法print_whatever("The first way to call the function.")print_whatever(123456789)print_whatever("The third way. %s" % "Third")i = 123456789print_whatever(i)def plus(x, y): return x + y print_whatever(plus(12, 34))in_put = raw_input("Enter something: \n")print_whatever(in_put)print_whatever(i + 100000000)j = 11print_whatever(j + plus(12, 34))print_whatever(56 + plus(12, 34))print_whatever(plus(12, 34) + plus(56, 78))
Exercise 20
代码
from sys import argvscript, input_file = argvdef print_all(f): print f.read()def rewind(f): f.seek(0) def print_a_line(line_count, f): print line_count, f.readline() current_file = open(input_file)print "First let's print the whole file:\n"print_all(current_file)print "Now let's rewind, kind of like a tape."rewind(current_file)print "Let's print three lines:"current_line = 1print_a_line(current_line, current_file)current_line = current_line + 1print_a_line(current_line, current_file)current_line = current_line + 1print_a_line(current_line, current_file)
输出
Notes:
①文件对象也可以作为函数的参数参与函数的执行过程
② += 是一种简写,类似的有 x -= y; x *= y; x /= y; x %= y
x += y #即x = x + y
③seek()用来调整文件操作标记的位置,关于文件及文件夹的操作笔记中已有记录
Exercise 21
代码
def add(a, b): print "Adding %d + %d" % (a, b) return a + bdef subtract(a, b): print "SUBTRACTING %d - %d" % (a, b) return a - b def multiply(a, b): print "MULTIPLYING %d * %d" % (a, b) return a * b def divide(a, b): print "DIVIDING %d / %d" % (a, b) return a / b print "Let's do some maths with just functions!"age = add(20, 3)height = subtract(180, 5)weight = multiply(65, 2)iq = divide(100, 2)print "Age: %d, Height: %d, Weight: %d, Iq: %d." % (age, height, weight, iq)# A puzzle for the extra credit, type it anyway.print "Here is a puzzle."what = add(age, subtract(height, multiply(weight, divide(iq, 2))))print "That becomes: ", what, "Can you do it in hand?"
输出
Notes:
①注意return的用法。它可以用于把函数的返回值赋值给一个变量。