Day4
-
习题 19: 函数和变量
1 def cheese_and_crackers(cheese_count, boxes_of_crackers):
2 print("You have %d cheeses!" % cheese_count)
3 print("You have %d boxes of crackers!" % boxes_of_crackers)
4 print("Man that's enough for a party!")
5 print("Get a blanket.\n")
6
7 print("We can just give the function numbers directly:")
8 cheese_and_crackers(20,30)
9
10 print("OR,we can use variables from our script:")
11 amount_of_cheese = 10
12 amount_of_crackers = 50
13
14 cheese_and_crackers(amount_of_cheese, amount_of_crackers)
15
16 print("We can even do math inside too:")
17 cheese_and_crackers(10+20, 5+6)
18
19 print("And we can combine the two, variables and math:")
20 cheese_and_crackers(amount_of_cheese + 100,
21 amount_of_crackers + 1000)
结果:
We can just give the function numbers directly:
You have 20 cheeses!
You have 30 boxes of crackers!
Man that's enough for a party!
Get a blanket.
OR,we can use variables from our script:
You have 10 cheeses!
You have 50 boxes of crackers!
Man that's enough for a party!
Get a blanket.
We can even do math inside too:
You have 30 cheeses!
You have 11 boxes of crackers!
Man that's enough for a party!
Get a blanket.
And we can combine the two, variables and math:
You have 110 cheeses!
You have 1050 boxes of crackers!
Man that's enough for a party!
Get a blanket.
自己编一个函数:
1 def pingfang(x):
2 return x*x
3
4 print(pingfang(5))
5
6 y=pingfang(6)
7 print(y)
25
36
-
习题 20: 函数和文件
1 from sys import argv
2
3 #脚本名,参数一(导入的文件)
4 script, input_file = argv
5
6 def print_all(f): #定义一个新函数:将文件全打印出来
7 print(f.read())
8
9 def rewind(f): #定义新函数:将文件指针移动到最开头
10 f.seek(0)
11
12 def print_a_line(line_count, f): #定义新函数:打印(行号,这行的内容)
13 print(line_count, f.readline())
14
15 current_file = open(input_file) #定义变量,打开导入的文件为当前文件
16
17 print("First let's print the whole file:\n")
18
19 print_all(current_file)
20
21 print("Now let's rewind, kinds of like a tape.")
22 rewind(current_file)
23
24 print("Let's print three lines:")
25
26 current_file = 1
27 print_a_line(current_file, current_file)
28
29 current_file = current_file + 1
30 print_a_line(current_file, current_file)
31
32 current_file = current_file + 1
33 print_a_line(current_file, current_file)
-
习题 21: 函数可以返回东西
1 def add(a, b):
2 print("ADDING %d + %d" % (a, b))
3 return a + b
4
5 def subtract(a, b):
6 print("SUBTRACTING %d - %d" % (a, b))
7 return a - b
8
9 def multipy(a, b):
10 print("MULTIPLAYING %d * %d" % (a, b))
11 return a * b
12
13 def divide(a, b):
14 print("DIVIDING %d / %d" % (a, b))
15 return a / b
16
17 print("Let's do some math with just functions!")
18
19 age = add(30, 5)
20 height = subtract(78,4)
21 weight = multipy(90,2)
22 iq = divide(100,2)
23
24 print("Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq))
25
26 #A puzzle for the extra credit, type it in anyway.
27 print("Here is a puzzle.")
28
29 what = add(age, subtract(height, multipy(weight, divide(iq,2))))
30
31 print("That becomes:\n",what,"\nCan you do it by hand?")
Let's do some math with just functions!
ADDING 30 + 5
SUBTRACTING 78 - 4
MULTIPLAYING 90 * 2
DIVIDING 100 / 2
Age: 35, Height: 74, Weight: 180, IQ: 50
Here is a puzzle.
DIVIDING 50 / 2
MULTIPLAYING 180 * 25
SUBTRACTING 74 - 4500
ADDING 35 + -4426
That becomes:
-4391.0
Can you do it by hand?
谜题解答
1 def jia(a,b,c,d,e):
2 print("谜题答案,一口气全算对\n %d + (%d - %d * %d / %d)" % (a,b,c,d,e))
3 return a+(b-c*d/e)
4
5 this=jia(35,74,180,50,2)
6 print(this)
7 print(this==what)
谜题答案,一口气全算对
35 + (74 - 180 * 50 / 2)
-4391.0
True
来源:oschina
链接:https://my.oschina.net/u/4389765/blog/4236943