这是我正在运行的代码:
def infinite_Third() -> Generator:
num = 1
while True:
if num % 3 ==0:
i = (yield num)
if i is not None:
num = i
num += 1
if __name__=='__main__':
third_gen = infinite_Third()
for i in third_gen:
print(f"it is {i}")
if i>1000:
break
third_gen.send(10*i+1)
我希望看到的结果是:
it is 3
it is 33
it is 333
it is 3333
但是,我真正得到的是:
it is 3
it is 36
it is 366
it is 3666
我认为这可能与在主代码中使用send
有关,但无法弄清楚原因。谁能帮忙?
根据我的评论,我已经修改了您的主循环
我设法通过增加infinite_Third()
的产量来使它按预期工作,但坦率地说,我不知道为什么会这样。