TypeError: '>' not supported between insta

2020-06-30 04:22发布

问题:

I'm working on a library for calculating certain values in a game. I have this code:

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)

When I try to run it, it raises this error:

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'

What's causing this error?

回答1:

You have a lot of issues where you don't distinguish between lists and integers.

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

billion[0] is not actually a 1000000 * 1000, its a length 1000 list.

This is the root of all your problems, since now suffix[0] becomes a list after the first iteration through your loop. The biggest change you needed was as follows:

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"]

This makes sure that each of these is a two element list with the proper suffix and value. Here it is all together:

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)

Output:

$14000000000000000000000000Sp


回答2:

Your million/billion/... variables are all lists. Send in million[0] to specify the int 1000000 in the 0th index of the list million and you get an answer out '$14000000000000T'

Also, when you * a list you are just copying out that list how ever many times you multiply it by. So your million * 1000 needs to be million[0] * 1000 and so on.



回答3:

Look at this.

>>> 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', ...

I don't think that's what you wanted.

I suggest something like this

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)

Whose sample output here is

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

But the idea is that you actually have named variables rather than just going on list indices where you seem not to know what types of variables are there.

For your purposes,

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