Ex24 More Practes

热身运动

Ex22,Ex23是回顾课程,所以没有具体的代码练习。从Ex24开始,进入新阶段的练习。Ex24和Ex25属于热身运动,综合前面的知识点,代码量**“稍大”**.

示例代码 Ex24:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
print "Let's practice everything."
print 'You\'d beed to know \' about escapes with \\ that do \n newlines and \t tab.'

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 "----------"
print poem
print "----------"

five = 10 -2 + 3 -6
# number can be print as string
print "This should be five: %s" % five
# The function can return more one result
def secret_formula(started):
	jelly_beans = started * 500
	jars = jelly_beans / 1000
	crates = jars / 100
	return jelly_beans, jars, crates 

start_point = 10000
# Assign more than one variables
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. 函数返回值,可以是多个。
  2. 格式化变量相互转化。
  3. 转义字符用法。
Licensed under CC BY-NC-SA 4.0
使用 Hugo 构建
主题 StackJimmy 设计