练习24:代码巩固练习

来源:互联网 发布:医保网络业务申请表 编辑:程序博客网 时间:2024/05/17 22:52
print "Let's practice everything."print 'you\'d need to know \' bout escapes with \\ that do \n newlines and \t tabs.'poem = """\tThe lovely worldwith logic so firmly plantedcannot discern \n the needs of lovenor comprehend passion from intuitionand requires an explantion\n\t\twhere there is none."""print "--------------"print poemprint "--------------"five = 10 - 2 + 3 - 6print "This should be five: %s" % fivedef secret_formula(started):        jelly_beans = started * 500        jars = jelly_beans / 1000        crates = jars / 100        return jelly_beans, jars, cratesstart_point = 10000beans, jars, crates = secret_formula(start_point)print "With a starting point of: %d" % start_pointprint "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates)start_point = start_point / 10print "We can also do that this way:"print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)

这里涉及到一些原来没见过的代码句式

beans, jars, crates = secret_formula(start_point)

如上行代码,当函数有多个返回值,可以在等号左边用逗号分隔的方式写上多个变量(与函数返回值数目一致),返回值会依次赋值给各个变量(就和解包argv时用的方法差不多)

print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)

如上行代码,可以直接用格式化字符输出函数的返回值,格式化字符数目和类型应该符合函数返回值数目和类型

0 0
原创粉丝点击