maple412 发表于 2014-11-03 17:46

yield的使用问题

通过下面的代码打印随机数
from random import randint
def randGen(alist):
    while len(alist) > 0:
      yield alist.pop(randint(0,len(alist)))

for item in randGen(['rock','paper','scissors']):
    print item


执行报错,错误提示:

maple412 发表于 2014-11-03 17:46

   错误提示:
yield alist.pop(randint(0,len(alist)))
IndexError: pop index out of range

Linux_manne 发表于 2014-11-04 10:14

本帖最后由 Linux_manne 于 2014-11-04 10:15 编辑

晕 你这个randint(0,len(alist)) 应该要减1阿
页: [1]
查看完整版本: yield的使用问题