zoukankan      html  css  js  c++  java
  • Python错误——TypeError: 'NoneType' object is not iterable

    问题描述:

    运行代码

    from random import randrange, randint, sample
    
    
    def display(balls):
        """
        输出列表中的双色球号码
        """
        for index, ball in enumerate(balls):
            if index == len(balls) - 1:
                print('|', end=' ')
            print('%02d' % ball, end=' ')
        print()
    
    
    def random_select():
        """
        随机选择一组号码
        """
        red_balls = [x for x in range(1, 34)]
        selected_balls = []
        selected_balls = sample(red_balls, 6)
        selected_balls.sort()
        selected_balls.append(randint(1, 16))
    
    
    
    def main():
        n = int(input('机选几注: '))
        for _ in range(n):
            display(random_select())
    
    
    if __name__ == '__main__':
        main()
    

      时出现该错误。

    问题分析:

    这又是一个典型的类型错误,其错误显示:‘NoneType’对象不可迭代。

    经过查找资料,发现本错误是由 遍历对象为None 导致的。

    知道原因后检查代码,会发现第30行所使用的random_select()出现于上一个函数中,且在上一个函数中并未对其进行return,导致其值为‘None’。

    所以在第23行代码后增加

    return select_balls
    

      问题解决~

  • 相关阅读:
    第06组Alpha冲刺(4/6)
    第06组Alpha冲刺(3/6)
    第06组Alpha冲刺(2/6)
    第06组 Alpha冲刺 (1/6)
    08-js函数
    07-数组
    06-js分支
    05-js运算符
    04-js变量
    03-css3D转换
  • 原文地址:https://www.cnblogs.com/qingyun-guo/p/13259251.html
Copyright © 2011-2022 走看看