我正在尝试根据用户输入构建一些数学运算的管道,并尝试打印该运算的累积结果。例如,输入将是一个操作列表,然后是数字输入的数量,然后是如下所示的数字:
[square, accumulate]
3
1
2
3
这应该返回如下内容:
1
5
14
首先,它将 1 打印为 1*1,然后将该 1 与 2*2 给出 5 的结果相加,然后将其添加到 3*3 中,给出 14。但是我的方法有问题,第二个数字输入总是变成None值,我不知道为什么。我被困在得到:
1
None
10
有什么想法吗?这是我的代码:
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)
猛跑小猪
慕森卡
斯蒂芬大帝
相关分类