Python Forum
error in Linking two coroutines
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error in Linking two coroutines
#1
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
Reply
#2
try like below

x=yield
    try:
        equation1.send(x)
    except:
        pass
    finally:
        equation2.send(x)
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
def main(x):
    n=numberParser()
    try:
        n.send(x)
    except StopIteration as e:
        pass
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  linking with excel mhmdbarazi 1 2,007 Apr-23-2020, 04:18 AM
Last Post: buran
  Linking python to Google Sheets Charliefish1311 1 2,247 Jul-09-2019, 12:31 AM
Last Post: micseydel
  Linking with multiple python libraries poijuggler 2 3,689 Oct-13-2017, 02:52 PM
Last Post: poijuggler
  coroutines Skaperen 9 6,595 Feb-04-2017, 11:17 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020