习题21、函数可以返回东西
#!/usr/bin/python
# -*- coding:utf-8 -*-
def add(a, b):
print "ADDING %d + %d" % (a, b)
return a + b
def subtract(a, b):
print "SUBTRACTING %d - %d" % (a, b)
return a - b
def multiply(a, b):
print "MULTIPLYING %d * %d" % (a, b)
return a * b
def divide(a, b):
print "DIVIDING %d / %d" % (a, b)
return a / b
print "Let's do some math wit just functions!"
age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)
print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)
# A puzzle for the extra credit , type it in anyway.
print "Here is a puzzle."
what = add(age, subtract(height, multiply(weight, divide(iq,2))))
print "That becomes: ", what, "Can you do it by hand?"
小结
- 为什么 Python 会把函数或公式倒着打印出来?
其实不是倒着打印,而是自内而外打印。如果你把函数内容逐句看下去,你会发现这里的规律。试着搞清楚为什么说它是“自内而外”而不是“自下而上”。- 怎样使用
raw_input()
输入自定义值?
记得int(raw_input())
吧?不过这样也有一个问题,那就是你无法输入浮点数,所以你可以试着使用float(raw_input())
。- 你说的“写一个公式”是什么意思?
来个简单的例子吧:24 + 34 / 100 - 1023
——把它用函数的形式写出来。然后自己想一些数学式子,像公式一样用变量写出来。
习题22、到现在你学到了哪些东西?
习题23、读代码
习题24、更多练习
#!/usr/bin/python
# -*- coding:utf-8 -*-
print "Let's practice everything."
print 'You\'d need to konw \'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 fromm intuition
and requires an explantion
\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." % secret_formula(start_point)
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)
小结
- 为什么你在后面把
jelly_beans
这个变量名又叫成了beans
?
这是函数的工作原理。记住函数内部的变量都是临时的,当你的函数返回以后,返回值可以被赋予一个变量。我这里是创建了一个新变量,用来存放函数的返回值。- 倒着阅读代码是什么意思?
从最后一行开始,把你写的和我写的代码进行比较。如果这一行完全一样,就接着比较上一行,直到全部比较完为止。
习题25、更多更多的练习
#!/usr/bin/python
# -*- coding:utf-8 -*-
def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words
def sort_words(words):
"""Sorts the words."""
return sorted(words)
def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.pop(0)
print word
def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1)
print word
def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)
def print_first_and_last(sentence):
"""Print the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)
def print_first_and_last_sorted(sentence):
"""Sorts the words then prints the first and last one."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)
运行截图: