新手学python-笨方法学python-ex19-函数和变量

跟着笨方法学python个教程学习,边学边做习题边记录。

新手学python--ex19-函数和变量 - 学习记录

课程内容:

函数这个概念也许承载了太多的信息量,不过别担心。只要坚持做这些练习,对照上个练习中的检查点检查一遍这次的联系,你最终会明白这些内容的。

有一个你可能没有注意到的细节,我们现在强调一下:函数里边的变量和脚本里边的变量之间是没有连接的。下面这个练习可以让你对这一点有更多的思考:

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)


通过这个练习,你看到我们给我们的函数 cheese_and_crackers 很多的参数,然后在函数里把它们打印出来。我们可以在函数里用变量名,我们可以在函数里做运算,我们甚至可以将变量和运算结合起来。

从一方面来说,函数的参数和我们的生成变量时用的 = 赋值符类似。事实上,如果一个物件你可以用 = 将其命名,你通常也可以将其作为参数传递给一个函数。

运行该程序:


$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.


$

 

 

加分习题:
1.倒着将脚本读完,在每一行上面添加一行注解,说明这行的作用。

2.从最后一行开始,倒着阅读每一行,读出所有的重要字符来。

3.自己编至少一个函数出来,然后用10种方法运行这个函数。

没想到10种,先这么着吧:

def newsum(augend , added):
    sum = augend + added
    print "\nThe Augend is %d\nThe added is %d \nThe sum of %d and %d is %d." % (augend, added, augend, added, sum)
    print "It is say that %d + %d = %d."  % (augend, added, sum)

print """
********************1********************
We can just give the function numbers directly:
"""
newsum(10, 20)

print """
********************2********************
OR, we can use variables from our script:
"""
num1 = 100
num2 = 200
newsum(num1, num2)

print """
********************3********************
We can even do math inside too:
"""
newsum(100 + 10, 200 + 20)

print """
********************4********************
And we can combind the two, variables and math:
"""
newsum(num1 + 1, num2 + 2)

print """
********************5********************
We can set the variables inputed by user
"""
n1 = int(raw_input("Pls input augend: "))
n2 = int(raw_input("Pls input addes: "))
newsum(n1, n2)

print """
********************6********************
We can set the variables inputed by user,and math
"""
newsum(n1 + 1000, n2 + 5000)

print """
********************7********************
We can use argv to do this:"""
from sys import argv

script, x, y = argv
newsum(int(x), int(y))

print """
********************8********************
We can use argv and math to do this:""" 
newsum(int(x) - 100, int(y) - 200)

#print """
#********************9********************
#Also, we can get the the function parameter from file
#"""

 

运行该程序:

$python ex19-3.py 100 200

********************1********************
We can just give the function numbers directly:


The Augend is 10
The added is 20
The sum of 10 and 20 is 30.
It is say that 10 + 20 = 30.

********************2********************
OR, we can use variables from our script:


The Augend is 100
The added is 200
The sum of 100 and 200 is 300.
It is say that 100 + 200 = 300.

********************3********************
We can even do math inside too:


The Augend is 110
The added is 220
The sum of 110 and 220 is 330.
It is say that 110 + 220 = 330.

********************4********************
And we can combind the two, variables and math:


The Augend is 101
The added is 202
The sum of 101 and 202 is 303.
It is say that 101 + 202 = 303.

********************5********************
We can set the variables inputed by user

Pls input augend: 5000
Pls input addes: 6000

The Augend is 5000
The added is 6000
The sum of 5000 and 6000 is 11000.
It is say that 5000 + 6000 = 11000.

********************6********************
We can set the variables inputed by user,and math


The Augend is 6000
The added is 11000
The sum of 6000 and 11000 is 17000.
It is say that 6000 + 11000 = 17000.

********************7********************
We can use argv to do this:

The Augend is 100
The added is 200
The sum of 100 and 200 is 300.
It is say that 100 + 200 = 300.

********************8********************
We can use argv and math to do this:

The Augend is 0
The added is 0
The sum of 0 and 0 is 0.
It is say that 0 + 0 = 0.

$

 

至此,笨方法学-ex19-函数和变量这一小节的学习结束了。

Thursday, November 05, 2015 | Python

文章评论

No comments posted yet.

发表评论

Please add 5 and 6 and type the answer here: