笨办法学Python ex21

函数的返回值


  • 输入:
# -*- coding: utf-8 -*-

def add(a, b):
    print "ADDING %d + %d" % (a, b)
    return a + b # 返回a+b的值
    
def subtract(a, b):
    print "SUBTRACTING %d - %d" % (a, b)
    return a - b

def multiply(a, b):
    print "MULTIPYING %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 with just functions!"

age = add(30, 5) # 调用函数获得一个值赋予age
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)
# 依次通过调用上面的‘+、-、*、/’,打印出年龄、身高、体重以及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))))
# 由内而外得调用函数获得一个数值来赋予what

print "That becomes:", what, "Can you do it by hand?"
  • 运行:


    笨办法学Python ex21_第1张图片

你可能感兴趣的:(笨办法学Python ex21)