1 # -*-coding:utf-8 -*- 2 def add(a, b): 3 print ("ADDING %d + %d" % (a, b)) 4 return a + b #要理解return的功能 5 6 def subtract(a, b): 7 print ("SUBSTRACTING %d - %d" % (a, b)) 8 return a - b 9 10 def multiply(a, b): 11 print ("MULTIPLYING %d * %d" % (a, b)) 12 return a * b 13 14 def divide(a, b): 15 print ("DIVIDING %d / %d" % (a, b)) 16 return a / b 17 18 print ("Let‘s do some math with just functions!") 19 20 age = add(30, 5) 21 height = subtract(78, 4) 22 weight = multiply(90, 2) 23 iq = divide(100, 2) 24 25 print ("Age: %d, Height:%d, Weight: %d, IQ: %d" % (age, height, weight, iq)) 26 27 #A puzzle for the extra credit, type it in anyway. 28 print ("Here is a puzzle.") 29 30 #what = add(age, subtract(height, multiply(weight, divide(iq, 2)))) 和下面的4行代码是一样的效果 31 divide1 = divide(iq, 2) 32 multiply1 = multiply(weight, divide1) 33 subtract1 =subtract(height, multiply1) 34 what = add(age, subtract1) 35 36 print ("That becomes:", what, "can you do it by hand?")
时间: 2024-11-02 11:03:24