1. 程式人生 > >python3 _笨方法學Python_日記_DAY4

python3 _笨方法學Python_日記_DAY4

圖片 變量 腳本 man day his http input log

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

python3 _笨方法學Python_日記_DAY4