python - 如何解决 TypeError 的问题:传递给 numpy.ndarray.__format__ 的格式字符串不受支持
问题描述
我有以下代码,但输出给我一个错误。
possible_rolls = arr = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
roll_result = np.random.choice(possible_rolls,1,replace=True)
modified_result = roll_result + 11
action_succeeded = modified_result > 15
print("On a modified roll of {:d}, Alice's action {}.".format(modified_result, "succeeded" if action_succeeded else "failed"))
TypeError:传递给 numpy.ndarray 的格式字符串不受支持。格式
解决方案
那是因为modified_result
是一个数组,而不是一个数字:
possible_rolls = arr = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
roll_result = np.random.choice(possible_rolls,1,replace=True)
modified_result = roll_result + 11
action_succeeded = modified_result > 15
print(type(modified_result))
>>> <class 'numpy.ndarray'>
这将解决问题:
print("On a modified roll of {:d}, Alice's action {}.".format(modified_result[0], "succeeded" if action_succeeded else "failed"))
>>> On a modified roll of 13, Alice's action failed.
推荐阅读
- mongodb - 使用键和值创建对 mongodb 数据的反应路由获取请求,而不仅仅是 id
- excel - 如何使用vba为多列创建过滤器excel
- java - Netty 中的 io.netty.allocator.maxOrder 是什么?
- javascript - 使这个异步/等待逻辑工作的麻烦
- flyte - 是否有从字符串创建 blob 类型的示例?
- python - 熊猫:获取独特的元素然后合并
- javascript - document.addEventListener 不在 JEST 的覆盖范围内
- typescript - 如何在本机反应中解锁一个屏幕上的旋转
- javascript - 我无法在主机上启动 node.js
- xamarin.forms - 如何在 Xamarin Forms UWP 上设置深色主题?