Python中文
首页
教程
问答
标签
搜索
登录
注册
len(列表)给出了TypeError:类型为“NoneType”的对象没有len()
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我试着制作一副牌,然后取出2 x 2并返回结果。 我希望避免“索引外”错误。 但我一直在讨论“typeError:objectoftype'NoneType'没有len()”错误。 正如我在这里检查的,它主要来自函数用法,原因是命令查询原则, 但我不认为这会发生在这里</p> <p>***代码***</p> <pre><code> def deck_creator(): suits = ("Hearts", "Diamonds", "Clubs", "Spades") values = ("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K") deck = [] for suit in suits: for value in values: card = value + " of "+ suit deck.append(card) return deck def card_dealer(deck): """ 5. Deal two cards to the Dealer and two cards to the Player """ print(type(deck)) # ==> !!! <class 'list'> !!! print(len(deck)) # ==> !!! 52 !!! dealers_cards = [] players_cards = [] shorter_then_two = True while shorter_then_two == True: if len(dealers_cards) < 2 or len(players_cards) < 2 : card_number = random.randint(1, (len(deck) + 1)) # typeError: object of type 'NoneType' has no len() card = deck[card_number] if len(dealers_cards) < 2: dealers_cards.append(card) else: players_cards.append(card) deck = deck.remove(card) else: shorter_then_two = False return players_cards, dealers_cards a = deck_creator() # print(a) b = card_dealer(a) # print(b) </code></pre> <p><em><strong>结果:</strong></em></p> <pre><code>card_number = random.randint(1, (len(deck) + 1)) TypeError: object of type 'NoneType' has no len() <class 'list'> 52 </code></pre> <p>我真的搞不清楚甲板列表在哪里变成了非类型对象。 谢谢你的帮助</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>这是因为您正在将值赋回<code>deck</code>变量,但<code>list.remove(index)</code>返回<code>None</code>,而不是更改列表</p> <pre><code>>>> l = [1, 2, 3] >>> l.remove(1) # None! >>> l [2, 3] </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在python中从数据帧列中删除分类值?
8 回答
如何在python中从数据帧列表中删除引号
5 回答
如何在python中从数据帧创建列表
8 回答
如何在Python中从数据帧创建嵌套的JSON
2 回答
如何在Python中从数据帧显示wordcloud
6 回答
如何在Python中从数据帧的时间戳中删除字符
5 回答
如何在Python中从数据帧绘制简单绘图?
4 回答
如何在python中从数据帧行提取具有特定长度的范围?
9 回答
如何在python中从数据帧设置dict中的值
1 回答
如何在Python中从数据库中获得一个结果
4 回答
如何在python中从数据框中绘制分类条形图
3 回答
如何在Python中从数据框中选择特定细节?
4 回答
如何在python中从数据集中删除unicode
9 回答
如何在python中从数据集中删除某些数值?
5 回答
如何在python中从数据集中选择行
5 回答
如何在Python中从数组中删除元素
8 回答
如何在python中从数组中删除单个倒逗号?
10 回答
如何在python中从数组中删除对象?
5 回答
如何在python中从数组中删除引号
9 回答
如何在python中从数组中删除所有最小值
3 回答