加入收藏 | 设为首页 | 会员中心 | 我要投稿 常州站长网 (https://www.0519zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

习题 19: 函数和变量

发布时间:2023-07-31 15:06:27 所属栏目:Asp教程 来源:转载
导读: 函数这个概念也许承载了太多的信息量,不过别担心。只要坚持做这些练习,对照上个练习中的检查点检查一遍这次的联系,你最终会明白这些内容的。
有一个你可能没有注意到的细节,我们现在强

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

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

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 很多的参数,然后在函数里把它们打印出来。我们可以在函数里用变量名,我们可以在函数里做运算ASP 变量,我们甚至可以将变量和运算结合起来。

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

你应该看到的结果

你应该研究一下脚本的输出,和你想象的结果对比一下看有什么不同。

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

【Python3的代码:

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_crakers = 50
cheese_and_crackers (amount_of_cheese, amount_of_crakers)

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_crakers + 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. 倒着将脚本读完,在每一行上面添加一行注解,说明这行的作用。

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

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

(编辑:常州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章