Python Forum
error in Linking two coroutines - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: error in Linking two coroutines (/thread-12811.html)



error in Linking two coroutines - shankar - Sep-14-2018

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



RE: error in Linking two coroutines - kalyan4343 - Oct-01-2018

try like below

x=yield
    try:
        equation1.send(x)
    except:
        pass
    finally:
        equation2.send(x)



RE: error in Linking two coroutines - ichabod801 - Oct-01-2018

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.


RE: error in Linking two coroutines - gogawaleankita - Jun-19-2020

def main(x):
    n=numberParser()
    try:
        n.send(x)
    except StopIteration as e:
        pass