-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathex21.py
executable file
·69 lines (44 loc) · 1.3 KB
/
ex21.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/python2
# -*- coding: utf-8 -*-
# ex21: Functions Can Return Something
def add(a, b):
print "ADDING %d + %d" % (a, b)
return a + b
def subtract(a, b):
print "SUBTRACTING %d - %d" % (a, b)
return a - b
def multiply(a, b):
print "MULTIPLYING %d * %d" % (a, b)
return a * b
def divide(a, b):
print "DIVIDING %d / %d" % (a, b)
return a / b
# my function to test return
def isequal(a, b):
print "Is %r equal to %r? - " % (a, b) ,
return (a == b)
print "Let's do some math with just functions!"
age = add(30, 5)
height = subtract(78, 4)
weight = multiply(92, 2)
iq = divide(100, 2)
print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)
# A puzzle for the extra credit, type it in anyway.
print "Here is a puzzle."
# switch the order of multiply and divide
what = add(age, subtract(height, divide(weight, multiply(iq, 2))))
print "That becomes: ", what, "Can you do it by hand?"
# test the return value of isequal()
num1 = 40
num2 = 50
num3 = 50
print isequal(num1, num2)
print isequal(num2, num3)
# A new puzzle.
print "Here is a new puzzle."
# write a simple formula and use the function again
uplen = 50
downlen = 100
height = 80
what_again = divide(multiply(height, add(uplen, downlen)), 2)
print "That become: ", what_again, "Bazinga!"