关于不可编码类型的Python错误

时间:2018-01-05 08:04:23

标签: python

  lvl=int(input("Level(1-15) => "))
  val=1 
  list(range(1, 16)) 

  if lvl != list and lvl >= list:
  print("Invalid Input. Please enter an integer value between 1 to 15")

错误讯息:

 Level(1-15) => 7
Traceback (most recent call last):
 File "./test.py", line 11, in <module>
if lvl != list and lvl >= list:
TypeError: unorderable types: int() >= type()

只是想知道为什么它会出现这种错误,但没有“和lvl&gt; = list”它可以正常工作

2 个答案:

答案 0 :(得分:0)

我认为你的代码实际上应该是这样编写的:

 lvl = int(input("Level(1-15) => "))

 ... 

 if lvl < 1 and lvl >= 16:
   print("Invalid Input. Please enter an integer value between 1 to 15")

注意:具体来说,当您删除and lvl >= list时,为什么代码似乎“正常”,实际上是因为list是类型{{1}的保留关键字}}:

list

因此,当您尝试比较整数 >>> list <type 'list'> 和类型lvl时,您的代码会自然出错。

答案 1 :(得分:0)

在Python中,list是一种数据类型。你做错了什么,你实际上是在试图看看5是否比书架上的书更重要。你想做的事:

lvl = int(input("Level(1-15) => "))
val = 1 # I don't know why this exists, but okay.

# If the value 'lvl' is not in the range of 1 - 15
if lvl not in range(1, 16): 
    print("Invalid Input. Please enter an integer value between 1 to 15")