码迷,mamicode.com
首页 > 其他好文 > 详细

ex21.py

时间:2016-11-21 12:08:29      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:ext   math   divide   code   tip   style   color   效果   span   

 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

标签:ext   math   divide   code   tip   style   color   效果   span   

原文地址:http://www.cnblogs.com/jiangzhipeng/p/6084864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!