i'm trying to build a pipeline of some math operations, based on user input and try to print the acumulation result for this operations. For example the input would be a list of operations, then the number of numeric inputs and then numbers like this:
[square, accumulate]
3
1
2
3
This should return something like:
1
5
14
First it prints 1 as 1*1, then it adds up that 1 to the result of 2*2 giving 5, then adds it to the 3*3 giving 14. But there's something wrong with my approach, the second number input always turns into a None value and I don't know why. I'm stuck getting:
1
None
10
Any ideas? Here is my code:
import math
import os
import random
import re
import sys
def printer():
while True:
x = yield
print(x)
def co_generator(n):
for _ in range(n):
x = int(input())
yield x
def get_root():
while True:
number = (yield)
yield math.floor(math.sqrt(number))
def get_square():
number = 0
while True:
number = (yield)
yield number**2
def accumulator():
acum = 0
while True:
acum += (yield)
yield acum
def operations_pipeline(numbers, operations, print_acum):
for num in numbers:
for i, w in enumerate(operations):
num = w.send(num)
print_acum.send(num)
for operation in operations:
operation.close()
print_acum.close()
if __name__ == '__main__':
order = input().strip()
n = int(input())
numbers = co_generator(n)
print_acum = printer()
next(print_acum)
root = get_root()
next(root)
accumulate = accumulator()
next(accumulate)
square = get_square()
next(square)
operations_pipeline(numbers, eval(order), print_acum)