From ba482e9e79fb5cc529dc2e8f354280b7d8964ad3 Mon Sep 17 00:00:00 2001 From: Eric Douglas Date: Thu, 22 May 2014 18:18:20 -0300 Subject: [PATCH] finish code example 1.1.3 --- .../UNIT-01/03-problem-solving/lec03.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/03-problem-solving/lec03.py b/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/03-problem-solving/lec03.py index 3e082a0..adc606b 100644 --- a/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/03-problem-solving/lec03.py +++ b/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/03-problem-solving/lec03.py @@ -46,7 +46,7 @@ if abs(ans**2 - x) >= epsilon: else: print ans, ' is close to square root of ', x """ - +""" x = 12345 epsilon = 0.01 numGuesses = 0 @@ -65,3 +65,20 @@ while abs(ans ** 2 - x) >= epsilon and ans <= x: print 'numGuesses = ', numGuesses print ans, ' is close to square root of ', x +""" + +def withinEpsilon(x, y, epsilon): + """ x, y, epsilon all ints or floats + returns true if x is within epsilon of y """ + + return abs(x - y) <= epsilon + +if withinEpsilon(25, 26, 1): + print 'yes' +else: + print 'no' + +if withinEpsilon(25, 26, 0.9): + print 'yes' +else: + print 'no'