使用发送方法时,为什么产量会跳过
guests = ["Tim,22",
"Tonya,45",
"Mary,12",
"Ann,32",
"Beth,20",
"Sam,5",
"Manny,76",
"Kenton,15",
"Kenny,27",
"Dixie,46",
"Mallory,32",
"Julian,4",
"Edward,71",
"Rose,65"]
def read_guestlist(file_name):
while True:
for person in guests:
name, age = person.split(",")
new_guest = yield name
if new_guest != None:
new_name, new_age = new_guest.split(",")
name = new_name
yield "This never gets printed"
yield new_name
guestlist = read_guestlist(guests)
counter = 0
for name in guestlist:
if counter == 5:
guestlist.send("Jane,35")
counter += 1
elif counter == 10:
guestlist.send("Pontus,35")
counter += 1
print(name)
counter += 1
if counter > 17:
break
我已经玩过此代码了一段时间,每次我都使用发送
一个收益> yart
被跳过。在这种情况下,这是yart
:屈服“这永远不会打印”
。我的问题是:为什么每次我使用发送
方法时,为什么特定的收益
都会跳过?
guests = ["Tim,22",
"Tonya,45",
"Mary,12",
"Ann,32",
"Beth,20",
"Sam,5",
"Manny,76",
"Kenton,15",
"Kenny,27",
"Dixie,46",
"Mallory,32",
"Julian,4",
"Edward,71",
"Rose,65"]
def read_guestlist(file_name):
while True:
for person in guests:
name, age = person.split(",")
new_guest = yield name
if new_guest != None:
new_name, new_age = new_guest.split(",")
name = new_name
yield "This never gets printed"
yield new_name
guestlist = read_guestlist(guests)
counter = 0
for name in guestlist:
if counter == 5:
guestlist.send("Jane,35")
counter += 1
elif counter == 10:
guestlist.send("Pontus,35")
counter += 1
print(name)
counter += 1
if counter > 17:
break
I have been playing around with this code for a while, and each time I have sent a value using send
one yield
gets skipped. In this case it is the yield
: yield "This never gets printed"
. My question is: why does that particular yield
get skipped each time I use send
method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论