Python Forum

Full Version: error in Linking two coroutines
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I couldnt get second output.I donno why
def coroutine_decorator(coroutine_func):
    def wrapper(*x,**y):
        c = coroutine_func(*x,**y)
        next(c)
        return c
    return wrapper

    
@coroutine_decorator
def linear_equation(a, b):
    x=yield
    c=a*(x**2)+b
    print("Expression, {}*x^2 + {}, with x being {} equals {}".format(a,b,x,c))
    

@coroutine_decorator
def numberParser():
    y=yield
    equation1 = linear_equation(3, 4)
    equation2 = linear_equation(2, -1)
    equation1.send(y)
    equation2.send(y)
def main(x):
    n = numberParser()
    n.send(x)
main(6)
Expected output
Output:
Expression, 3*x^2 + 4, with x being 6.0 equals 112.0 Expression, 2*x^2 + -1, with x being 6.0 equals 71.0
My output
Output:
Expression, 3*x^2 + 4, with x being 6.0 equals 112.0
Error
Error:
Traceback (most recent call last): File "solution.py", line 37, in <module> res = main(x); File "solution.py", line 30, in main n.send(x) File "solution.py", line 26, in numberParser equation1.send(y) StopIteration
try like below

x=yield
    try:
        equation1.send(x)
    except:
        pass
    finally:
        equation2.send(x)
Try using python tags. I did them for you this time, but check out the BBCode link in my signature below for instructions on how to do it yourself.
def main(x):
    n=numberParser()
    try:
        n.send(x)
    except StopIteration as e:
        pass