-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqrt.py
43 lines (32 loc) · 841 Bytes
/
sqrt.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
# Mystery computation in Python
# Takes input n and computes output named result
import simplegui
# global state
result = 1
iteration = 0
max_iterations = 10
# helper functions
def init(start):
"""Initializes n."""
global n
n = start
print "Input is", n
def get_next(current):
"""??? Part of mystery computation."""
return 0.5 * (current + n / current)
# timer callback
def update():
"""??? Part of mystery computation."""
global iteration, result
iteration += 1
# Stop iterating after max_iterations
if iteration >= max_iterations:
timer.stop()
print "Output is", result
else:
result = get_next(result)
# register event handlers
timer = simplegui.create_timer(1, update)
# start program
init(13)
timer.start()