最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

習(xí)題 19: 函數(shù)和變量?

函數(shù)這個概念也許承載了太多的信息量,不過別擔(dān)心。只要堅(jiān)持做這些練習(xí),對照上個練習(xí)中的檢查點(diǎn)檢查一遍這次的聯(lián)系,你最終會明白這些內(nèi)容的。

有一個你可能沒有注意到的細(xì)節(jié),我們現(xiàn)在強(qiáng)調(diào)一下:函數(shù)里邊的變量和腳本里邊的變量之間是沒有連接的。下面的這個練習(xí)可以讓你對這一點(diǎn)有更多的思考:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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_cheese = 10
amount_of_crackers = 50

cheese_and_crackers(amount_of_cheese, 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, variables and math:"
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

通過這個練習(xí),你看到我們給我們的函數(shù) cheese_and_crackers 很多的參數(shù),然后在函數(shù)里把它們打印出來。我們可以在函數(shù)里用變量名,我們可以在函數(shù)里做運(yùn)算,我們甚至可以將變量和運(yùn)算結(jié)合起來。

從一方面來說,函數(shù)的參數(shù)和我們的生成變量時(shí)用的 = 賦值符類似。事實(shí)上,如果一個物件你可以用 = 將其命名,你通常也可以將其作為參數(shù)傳遞給一個函數(shù)。

你應(yīng)該看到的結(jié)果?

你應(yīng)該研究一下腳本的輸出,和你想象的結(jié)果對比一下看有什么不同。

$ python ex19.py
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.
$

加分習(xí)題?

  1. 倒著將腳本讀完,在每一行上面添加一行注解,說明這行的作用。
  2. 從最后一行開始,倒著閱讀每一行,讀出所有的重要字符來。
  3. 自己編至少一個函數(shù)出來,然后用10種方法運(yùn)行這個函數(shù)。

Project Versions

Table Of Contents

Previous topic

習(xí)題 18: 命名、變量、代碼、函數(shù)

Next topic

習(xí)題 20: 函數(shù)和文件

This Page

邹平县| 宾川县| 子洲县| 武邑县| 南通市| 田林县| 五台县| 海口市| 浮梁县| 珠海市| 钟祥市| 长乐市| 永仁县| 裕民县| 宽甸| 会泽县| 温泉县| 新建县| 封丘县| 牟定县| 崇信县| 晋宁县| 和平县| 上蔡县| 本溪| 博罗县| 丹阳市| 普兰县| 抚州市| 元朗区| 且末县| 清苑县| 惠来县| 永修县| 昌吉市| 新巴尔虎左旗| 芜湖市| 铜川市| 霍林郭勒市| 古丈县| 化隆|