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

时间:2022-04-26
本文章向大家介绍《笨办法学Python》 第24课手记,主要内容包括《笨办法学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
nttwhere 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)

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

结果如下:

本节课涉及的知识

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