笔记11 笨办法学python之练习19各种传递函数值的方式

笔记11 笨办法学python之练习19各种传递函数值的方式

开始练习19了,继续理解python的函数,这个19,只有一个函数定义,这个函数被定义为:cheese_and_crackers,显然是个有关食品的函数。两个参数,一个是cheese_count,另一个是boxes_of_crackers。
这次录下编码,一个错都没有。在ps上,一次执行成功,结果是几段有关函数与食品数量的对话。教材告知,这段编码展示的是,函数传递值的各种方式。
练习ex19.py的编码与执行
def cheese_and_crackers(cheese_count, boxes_of_crackers):
print(f"you have {cheese_count} cheeses!")
print(f"you have {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)

在这里插入代码片

我试着给每段编码一个注释,以理解到底有多少种传递函数值的方式。
带注释的ex19.py,看来至少有四种传递数值的方式,也许更多。

#定义一个带两个参数的函数
def cheese_and_crackers(cheese_count, boxes_of_crackers):
    #用指令F 使用花括号调用参数cheese_count,奶酪的数目
    print(f"you have {cheese_count} cheeses!")
    #用指令f,使用花括号调用参数boxes_of_crackers,饼干包数量
    print(f"you have {boxes_of_crackers} boxes_of_crackers!")
    #调用字符串表示对于数量的态度
    print("Man that's enough for a party!")
    #补充语,数量够了,但得加一个毯子
    print("Get a blanket.\n")

#print不缩进的指令,应该是置于首句的
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)
这里插入代码片

把ex19改造为在终端填上数值的方式,这就成为ex19.2.py,这也是可行的。
改为终端填数字的ex19.2.py

在这里插入代码片
```#定义一个带两个参数的函数
def cheese_and_crackers(cheese_count, boxes_of_crackers):
    #用指令F 使用花括号调用参数cheese_count,奶酪的数目
    print(f"you have {cheese_count} cheeses!")
    #用指令f,使用花括号调用参数boxes_of_crackers,饼干包数量
    print(f"you have {boxes_of_crackers} boxes_of_crackers!")
    #调用字符串表示对于数量的态度
    print("Man that's enough for a party!")
    #补充语,数量够了,但得加一个毯子
    print("Get a blanket.\n")

#print不缩进的指令,应该是置于首句的
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)


脚本ex19.2.py终端执行结果如下:
PS C:\Users\lenovo\1pythonw> python ex19.2.py
We can just give the function numbers directly:
cheese_count:20
boxes_of_crackers:30
OR, we can use variables from our script:
amount_of_cheese:10
amount_of_crackers:50
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 2010 cheeses!
you have 3050 boxes_of_crackers!
Man that's enough for a party!
Get a blanket.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值