笨方法学Python 习题21

函数可以返回某些变量

def add(a,b):
    print("Adding {0} + {1}".format(a,b))
    return(a+b)

def subtract(a,b):
    print("SUBTRACT {0} - {1}".format(a,b))
    return(a-b)

def multiply(a,b):
    print("MULTIPLYING {0} * {1}".format(a,b))
    return(a*b)

def divide(a,b):
    print("DIVIDING {0} / {1}".format(a,b))
    return(a/b)

print("Let's do some math with just functions")

age = add(30,5) 
height = subtract(78,4)
weight = multiply(90,2)
iq = divide(100,2)
# 在赋值四个变量的同时,输出四行

print("Age: {0}, Height: {1}, Weight: {2}, IQ: {3}".format(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?")

猜你喜欢

转载自blog.csdn.net/weixin_43254423/article/details/82830009
今日推荐