类型错误:"int"和"type"实例之间不支持"<"


T=[int]*100
N=int(input("Enter an integer "))
while not 2<=N<=100:
N=int(input("Enter an integer "))
for i in range(N):
T[i]=int(input("Enter strictly increasing numbers "))
while not int(T[i]) < int(T[i+1]):
T[i]=int(input("Enter strictly increasing numbers "))
for i in range(N):
if T[i]!=T[i+1]+1:
print(T[i]+1)

Traceback (most recent call last): File "C:/Users/WesternDigital/Desktop/probthlatha.py", line 7, in <module> while not T[int(i)]<T[int(i+1)]: TypeError: '<' not supported between instances of 'int' and 'type'

我试着用int(T[I](更改T[I],但这只会返回以下错误:(int((参数必须是字符串、类似字节的对象或实数,而不是"type"(

我猜不出第二个循环打算做什么,但以下是如何通过验证输入数字。请注意,我没有预先初始化数组;我在飞行中建造它。

T=[]
N=int(input("Enter an integer "))
while not 2<=N<=100:
N=int(input("Enter an integer from 2 to 100")) 
for i in range(N):
T.append( int(input("Enter strictly increasing numbers ") ))
while i and not T[i] > T[i-1]:
T[-1]=int(input("No, enter strictly increasing numbers "))
for i in range(N):
if i and T[i]!=T[i-1]+1:
print(T[i]+1)

这是因为T是一个列表,其中所有元素都是int。因此,当您比较T[i]T[i+1]时,其中一个是数字,但另一个仍然是int类。您应该将T初始化为一个数字数组,例如通过执行[0] * 100

无限循环的问题是由以下代码部分引起的:

for i in range(N):
T[i]=int(input("Enter strictly increasing numbers "))
while not int(T[i]) < int(T[i+1]): # << logical error here
T[i]=int(input("Enter strictly increasing numbers "))

您需要将您的条件更改为not T[i] > T[i-1]。这将修复这两个错误,因为程序引用的是以前在条件中输入的数字,而不是int类或未分配给的元素。

相关内容

最新更新