首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PythonMath-TypeError:‘NoneType’对象不可订阅怎么办?

PythonMath-TypeError:‘NoneType’对象不可订阅怎么办?
EN

Stack Overflow用户
提问于 2018-08-15 01:59:12
回答 2查看 0关注 0票数 0

我正在为数学做一个小程序(没有什么特别的原因,只是有点想要),我遇到了一个错误“TypeError:‘NoneType’对象是不可订阅的。

import math

print("The format you should consider:")
print str("value 1a")+str(" + ")+str("value 2")+str(" = ")+str("value 3a ")+str("value 4")+str("\n")

print("Do not include the letters in the input, it automatically adds them")

v1 = input("Value 1: ")
v2 = input("Value 2: ")
v3 = input("Value 3: ")
v4 = input("Value 4: ")

lista = [v1, v3]
lista = list.sort(lista)

a = lista[1] - lista[0]

list = [v2, v4]
list = list.sort(list)

b = list[1] = list[0]

print str(a)+str("a")+str(" = ")+str(b)

错误:

Traceback (most recent call last):
  File "C:/Users/Nathan/Documents/Python/New thing", line 16, in <module>
    a = lista[1] - lista[0]
TypeError: 'NoneType' object is not subscriptable
EN

回答 2

Stack Overflow用户

发布于 2018-08-15 10:20:43

异常TypeError:'NoneType'对象不可订阅,因为lista的值实际上是None。 如果你在Python命令行中尝试此操作,则可以重现你在代码中获得的TypeError:

None[0]

lista设置为None的原因是list.sort()的返回值为None .它不返回原始列表的排序副本。 相反,作为文件指出,列表就地排序而不是复制(这是出于效率的原因)。

如果你不想更改原始版本,可以使用

other_list = sorted(lista)
票数 0
EN

Stack Overflow用户

发布于 2018-08-15 11:18:52

lista = list.sort(lista)

这应该是

lista.sort()

The .sort()方法不返回任何值。

sorted_list = sorted(lista)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档