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问题
plt.savefig不会覆盖现有文件
8 回答
plt.savefig不保存图像
3 回答
plt.savefig在jupyter笔记本中不起作用
6 回答
plt.savefig在从另一个fi调用时停止工作
10 回答
plt.savefig在调用plt.show之前保存空数字
3 回答
plt.save不创建png文件
6 回答
plt.scatter overlay分类数据帧列
6 回答
Plt.Scatter:如何添加title、xlabel和ylab
7 回答
plt.scatter()绘图与Matplotlib中的plt.plot()绘图类似
9 回答
plt.scatter错误'NoneType'对象在成功运行后没有属性'sqrt'
5 回答
plt.set_title()中的标题字符串有误
4 回答
plt.show()
7 回答
plt.show()不在Jupyter笔记本上渲染任何内容
3 回答
plt.show()不打印plt.plot only plt.scatter
5 回答
plt.show()不显示三维散射图像
9 回答
plt.show()不显示任何内容
2 回答
plt.show()不显示数据,而是保留它供下一个图表使用(spyder)
7 回答
plt.show()使终端挂起
8 回答
plt.show()无法使用此代码
4 回答
plt.show()没有打开新的图形风
1 回答