0% found this document useful (1 vote)
3K views

Coroutine

The document defines a coroutine decorator function that initializes a coroutine function. It then defines two linear equation coroutine functions that calculate quadratic expressions. Finally, it defines a numberParser coroutine function that takes user input and sends it to the two linear equation coroutines to calculate results simultaneously.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
3K views

Coroutine

The document defines a coroutine decorator function that initializes a coroutine function. It then defines two linear equation coroutine functions that calculate quadratic expressions. Finally, it defines a numberParser coroutine function that takes user input and sends it to the two linear equation coroutines to calculate results simultaneously.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

def linear_equation(a, b):

while True:
x = yield
e = a*(x**2)+b
print('Expression, '+str(a)+'*x^2 + '+str(b)+', with x being '+str(x)+'
equals '+str(e))

================================================

# Define 'coroutine_decorator' below


def coroutine_decorator(coroutine_func):
def wrapper(*args, **kwdargs):
c = coroutine_func(*args, **kwdargs)
next(c)
return c
return wrapper

# Define coroutine 'linear_equation' as specified in previous exercise


@coroutine_decorator
def linear_equation(a, b):
while True:
x = yield
e = a*(x**2)+b
print('Expression, '+str(a)+'*x^2 + '+str(b)+', with x being '+str(x)+'
equals '+str(e))

=================================================

# Define the function 'coroutine_decorator' below


def coroutine_decorator(coroutine_func):
def wrapper(*args, **kwdargs):
c = coroutine_func(*args, **kwdargs)
next(c)
return c
return wrapper

# Define the coroutine function 'linear_equation' below


@coroutine_decorator
def linear_equation(a, b):
while True:
x = yield
e = a*(x**2)+b
print('Expression, '+str(a)+'*x^2 + '+str(b)+', with x being '+str(x)+'
equals '+str(e))

# Define the coroutine function 'numberParser' below


@coroutine_decorator
def numberParser():
equation1 = linear_equation(3, 4)
equation2 = linear_equation(2, -1)
# code to send the input number to both the linear equations
while True :
x = yield
equation1.send(x)
equation2.send(x)
def main(x):
n = numberParser()
n.send(x)

You might also like