Python 数学 - 类型错误 : 'NoneType' object is not subscriptable

标签 python math sorting in-place

我正在为数学编写一个小程序(没有特别的原因,只是有点想这样做),我遇到了错误“TypeError: 'NoneType' object is not subscriptable.

我以前从未见过这个错误,所以我不知道它是什么意思。

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

最佳答案

lista = list.sort(lista)

这应该是

lista.sort()

.sort() 方法是就地的,并返回 None。如果你想要一些不就地的东西,它会返回一个值,你可以使用

sorted_list = sorted(lista)

除了 #1:请不要将您的列表称为 list。这破坏了内置列表类型。

除了#2:我不确定这条线是什么意思:

print str("value 1a")+str(" + ")+str("value 2")+str(" = ")+str("value 3a ")+str("value 4")+str("\n")

是不是很简单

print "value 1a + value 2 = value 3a value 4"

?换句话说,我不知道你为什么要在已经是 str 的东西上调用 str。

除了 #3:有时您使用 print("something")(Python 3 语法),有时您使用 print "something"(Python 2)。后者会在 py3 中给你一个 SyntaxError,所以你必须运行 2.*,在这种情况下你可能不想养成这个习惯,否则你会用额外的括号打印元组。我承认它在这里可以很好地工作,因为如果括号中只有一个元素,它不会被解释为元组,但它在 python 眼中看起来很奇怪..

关于Python 数学 - 类型错误 : 'NoneType' object is not subscriptable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9320766/

相关文章:

python - 贝叶斯神经网络 : Computation of Hessian

math - 无尺寸的 2D 到 1D 转换

python - 获取缓存结果后的 Django/Python 排序

Python 输出重叠

python - 查找包含特定字符的字符串的单元格数量

python - 应用引擎 : string to datetime?

c++ - 完美的正方形和完美的立方体

C 中的凯撒密码 : can't seem to wrap around the latter letters of the alphabet

java - 给定一个输入中的两个字符串(用逗号分隔),如何在 Java 中找出字符串 S1 的排列是否等于字符串 S2?

java - 如何在java中按字母顺序排列组合框? (使用 Netbeans IDE 7.3)