我正在为数学制作一个小程序(没有特别的原因,只是有点想要),我遇到了错误“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
原文由 Fen 发布,翻译遵循 CC BY-SA 4.0 许可协议
这应该是
.sort()
方法是就地的,并返回无。如果你想要一些不就地的东西,它会返回一个值,你可以使用除了#1:请不要调用您的列表
list
。这破坏了内置列表类型。除了#2:我不确定这条线是什么意思:
是不是很简单
?换句话说,我不知道你为什么要在已经是 str 的东西上调用 str。
除了#3:有时你使用
print("something")
(Python 3 语法),有时你使用print "something"
(Python 2)。后者会在 py3 中给你一个 SyntaxError,所以你必须运行 2.*,在这种情况下你可能不想养成这个习惯,否则你会用额外的括号打印元组。我承认它在这里工作得很好,因为如果括号中只有一个元素,它不会被解释为元组,但它看起来很奇怪。