前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >习题19:函数和变量

习题19:函数和变量

作者头像
py3study
发布2018-08-02 14:39:01
3820
发布2018-08-02 14:39:01
举报
文章被收录于专栏:python3

函数里面的变量和脚本里面的变量之间是没有连接的,更多的函数练习

代码如下

代码语言:javascript
复制
# coding: utf-8
__author__ = 'www.py3study.com'
def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print("You have {} cheeses !".format(cheese_count))
    print("You have {} boxes of crackers !".format(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_cheese)
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 很多的参数,然后在函数里把它们打印出来,可以在函数里用变量名,可以在函数里做运算,甚至可以变量和运算结合起来

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

应该看到的结果

图片.png
图片.png

常见问题

怎么处理用户输入的数字,如果想让数量相加?

记住使用int() 把input()的值转为整数

可以在函数中调用函数吗?

可以,后面会用到

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/11/12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档