首页 > 代码库 > ex21.py
ex21.py
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?")
ex21.py
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。