Docsity

square root in python, Study Guides, Projects, Research of System Programming

Toggle sidebar
bg1
# 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(1024)
timer.start()

Partial preview of the text

Download square root in python and more Study Guides, Projects, Research System Programming in PDF only on Docsity!

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(1024) timer.start()