TypeError:'>' 'list'和'int'的实例之间不支持

时间:2018-03-25 02:58:57

标签: python python-3.x if-statement math calculator

我正在开发一个用于计算游戏中某些值的库。我有这段代码:

million = [1000000, "M"]
billion = [million * 1000, "B"]
trillion = [billion * 1000, "T"]
quadrillion = [trillion * 1000, "Qd"]
quintillion = [quadrillion * 1000, "Qn"]
sx = [quintillion * 1000, "Sx"]
septillion = [sx * 1000, "Sp"]

suffixes = [million, billion, trillion, quadrillion, quintillion, sx, septillion]

def getSetupResult(orevalue, furnacemultiplier, *upgrades, **kwargs):
    for i in upgrades:
        orevalue *= i
    orevalue *= furnacemultiplier
    for suffix in suffixes:
        if orevalue > suffix[0] - 1 and orevalue < suffix[0] * 1000:
            print("$"+str(orevalue)+suffix[1])

getSetupResult(quintillion,700,5,4,10,100)

当我尝试运行它时,会引发此错误:

Traceback (most recent call last):
  File "C:/Users/???/Desktop/MinersHavenCalculator.py", line 19, in <module>
    getSetupResult(quintillion,700,5,4,10,100)
  File "C:/Users/???/Desktop/MinersHavenCalculator.py", line 16, in getSetupResult
    if orevalue > suffix[0] - 1 and orevalue < suffix[0] * 1000:
TypeError: '>' not supported between instances of 'list' and 'int'

导致此错误的原因是什么?

3 个答案:

答案 0 :(得分:3)

你有很多问题,你不能区分列表和整数。

million = [1000000, "M"]
billion = [million * 1000, "B"]

billion[0]实际上不是1000000 * 1000,其长度为1000个列表。

这是所有问题的根源,因为现在suffix[0]在循环第一次迭代后成为一个列表。您需要的最大变化如下:

million = [1000000, "M"]
billion = [million[0] * 1000, "B"]
trillion = [billion[0] * 1000, "T"]
quadrillion = [trillion[0] * 1000, "Qd"]
quintillion = [quadrillion[0] * 1000, "Qn"]
sx = [quintillion[0] * 1000, "Sx"]
septillion = [sx[0] * 1000, "Sp"]

这确保了每个都是具有正确后缀和值的双元素列表。这一切都在一起:

suffixes = [million, billion, trillion, quadrillion, quintillion, sx, septillion]

def getSetupResult(orevalue, furnacemultiplier, *upgrades, **kwargs):
    for i in upgrades:
        orevalue *= i
    orevalue *= furnacemultiplier
    for suffix in suffixes:
        if orevalue > suffix[0] - 1 and orevalue < suffix[0] * 1000:
            print("$"+str(orevalue)+suffix[1])
getSetupResult(quintillion[0],700,5,4,10,100)

输出:

$14000000000000000000000000Sp

答案 1 :(得分:1)

您的百万/十亿/ ...变量都是列表。发送百万[0]以指定列表百万的第0个索引中的int 1000000,并得到答案&#39; $ 14000000000000T&#39;

另外,当你*一个列表时,你只是复制了那个列表,你多次乘以它。所以你的百万* 1000需要百万[0] * 1000等等。

答案 2 :(得分:0)

看看这个。

>>> million = [1000000, "M"]
>>> billion = [million * 1000, "B"]
>>> print(billion)
[[1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', 1000000, 'M', ...

我不认为这就是你想要的。

我建议这样的事情

class Number():
  def __init__(self, val, suffix):
    self.val = val
    self.suffix = suffix

million = Number(10**6, "M")
billion = Number(10**9, "B")
trillion = Number(10**12, "T")
quadrillion = Number(10**15, "Qd")
quintillion = Number(10**18, "Qn")
sx = Number(10**21, "Sx")
septillion = Number(10**24, "Sp")

numbers = [million, billion, trillion, quadrillion, quintillion, sx, septillion]

for n in numbers:
  print(n.val, n.suffix)

这里的样本输出是

1000000 M
1000000000 B
1000000000000 T
1000000000000000 Qd
1000000000000000000 Qn
1000000000000000000000 Sx
1000000000000000000000000 Sp

但是这个想法是你实际上已经命名变量,而不仅仅是列表索引,你似乎不知道那里有什么类型的变量。

出于您的目的,

for n in numbers:
    if orevalue > (n.val - 1) and orevalue < (n.val * 1000):
        print("${}{}".format(orevalue, n.suffix))