发布时间:2017-11-14 17:01:35编辑:Run阅读(5707)
练习代码如下
# coding: utf-8 __author__ = 'www.py3study.com' print("Let's practice everything.") print("You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.") poem = """ \tThe lovely world with logic so firmly planted cannot discern \n the needs of love nor comprehend passion from intuition and requires an explanation \n\t\twhere there is none. """ print('-'*50) print(poem) print('-'*50) five = 10 - 2 + 3 - 6 print("This should be five:{}".format(five)) def secret_formula(started): jelly_beans = started * 500 jars = jelly_beans / 1000 crates = jars / 100 return jelly_beans, jars, crates started_point = 10000 print(secret_formula(started_point)) beans, jars, crates = secret_formula(started_point) print("With a starting point of:{}".format(started_point)) print("We'd have {} beans, {} jars, and {} crates.".format(beans, jars, crates)) start_point = started_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))
应该看到的结果
常见问题
为什么你在后面把jelly_beans这个变量名又叫成了beans?
这是函数的工作原理,记住函数内部的变量都是临时的,当你的函数返回以后,返回值可以被赋予一个变量,这里是创建了一个新变量,用来存放函数的返回值
上一篇: 习题21:函数可以返回东西
下一篇: 习题23:更多更多的练习
47745
46237
37110
34627
29229
25886
24745
19863
19417
17909
5716°
6316°
5836°
5888°
6985°
5829°
5846°
6361°
6316°
7673°