程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

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

发布于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黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

6 0
收藏该文
已收藏

评论内容:(最多支持255个字符)