发布于2020-04-13 18:10 阅读(2183) 评论(0) 点赞(6) 收藏(1)
笔记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.
原文链接:https://blog.csdn.net/weixin_41670255/article/details/105448764
作者:heer
链接:https://www.pythonheidong.com/blog/article/328037/3079d89a286598c000e9/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!