有没有更优雅的办法来判断 for 循环是否运行
for 循环的集合是一个由包含 yield 语句函数创建的生成器,目前想到的一种办法是在循环外初始化一个变量,循环体内让这个变量自增,如果最后发现变量没有变化,就说明循环没有运行
def output_single_word():
for word in input():
yield word
count = 0
for i in output_single_word():
count += 1
print(i)
if count == 0:
print('for循环没有运行')
else:
print('for循环运行完毕')
不知道有没有不初始化 count 变量的更优雅的方法来判断 for 循环是否运行