博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Learn Python The Hard Way(19)
阅读量:6971 次
发布时间:2019-06-27

本文共 1626 字,大约阅读时间需要 5 分钟。

hot3.png

Functions may have been a mind-blowing amount of information, but do not worry. Just keep doing these exercises and going through your checklist from the last exercise and you will eventually get it.

There is one tiny point though that you might not have realized which we'll reinforce right now: The variables in your function are not connected to the variables in your script. Here's an exercise to get you thinking about this:

#!/usr/bin/env python# -*- coding: utf-8 -*-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 = 10amount_of_crackers = 50cheese_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)

result:

                     160429_GhlM_189901.jpg

This shows all different ways we're able to give our function cheese_and_crackers the values it needs to print them. We can give it straight numbers. We can give it variables. We can give it math. We can even combine math and variables. 

In a way, the arguments to a function are kind of like our = character when we make a variable. In fact, if you can use ‘=’ to name something, you can usually pass it to a function as an argument.

转载于:https://my.oschina.net/xtfjt1988/blog/401931

你可能感兴趣的文章
lamp 编译参数
查看>>
FastDFS之Binlog同步
查看>>
谷歌Quickoffice现免费面向所有用户
查看>>
我的友情链接
查看>>
Mvc Html.BeginForm 方式提交Form前验证
查看>>
初探KVM-第一个虚拟机
查看>>
Tomcat的安装和配置
查看>>
Lync Server 2010标准版系列PART5:安装部署
查看>>
log4j MDC NDC详解
查看>>
更改文件内容并保存
查看>>
我经常需要安装的Eclipse插件
查看>>
前端——css3动画总结
查看>>
ELP界的苹果:太奇pad开创教育电子产品新时代
查看>>
记录新机房建设。20130711
查看>>
主Makefile分析
查看>>
Java RMI之HelloWorld篇
查看>>
一张图看懂跨境电商的前世今生(附XMIND整理)
查看>>
NFS简要安装步骤与配置(debian/ubuntu)
查看>>
温度传感器+I2C+串口+PC上位机(pyserial)例子
查看>>
结合keepalived实现lvs的高可用群集故障自动转移
查看>>