《笨办法学Python》 第24课手记

《笨办法学Python》 第24课手记

本节课是前面所有课程的复习,请认真对待,也许你都理解这些代码的含义,那么请尽量努力一次通过,不要出现任何错误。如果你出现了错误,那说明你没有养成良好的输代码习惯,请从现在开始养成不会出现任何错误的习惯。

原代码如下:

print "Let's practice everything."
print 'You\'d need to know \'bout ecsape with \\ that do \n newlines and \t tabs.'

poem = '''
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of lovely
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none
'''


print "--------------"
print poem
print "--------------"


five = 10 - 2 + 3 - 6
print "This should be five: %s" % five

def secret_formula(started):
   jelly_beans = started * 500
   jars = jelly_beans / 1000
   crates = jars / 100
   return jelly_beans, jars, crates

start_point = 10000
beans, jars, crates = secret_formula(start_point)

print "with a starting point of :%d " % start_point
print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates)

start_point = start_point / 10

print "We can also do that this way:"
print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)

请留意最后一行,出现了新的用法,那就是不经过变量,直接以%+函数的形式格式化输出函数的结果,请记住这种用法,可以简化你的代码、

结果如下:

这里写图片描述

本节课涉及的知识

函数内部变量的作用于仅仅是函数内,对函数外或者其他函数内部则没有影响,因此可以有相同的变量名。请根据实际情况取舍,有时候相同的变量名会更简便,有时候则会引起混淆。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值