前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《笨办法学Python》 第24课手记

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

作者头像
Steve Wang
发布2018-02-05 16:31:56
3740
发布2018-02-05 16:31:56
举报
文章被收录于专栏:从流域到海域从流域到海域

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

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

原代码如下:

代码语言:javascript
复制
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)

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

结果如下:

这里写图片描述
这里写图片描述

本节课涉及的知识

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年01月26日,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 《笨办法学Python》 第24课手记
  • 本节课涉及的知识
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档