Home > database >  TypeError: '<' not supported between instances of 'int' and 'type'
TypeError: '<' not supported between instances of 'int' and 'type'

Time:03-29

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'

I tried changing T[i] with int(T[i]) but that would just return this error instead: (int() argument must be a string, a bytes-like object or a real number, not 'type')

CodePudding user response:

I can't guess what you intended that second loop to do, but here's how you can input your numbers with validation. Note that I don't pre-initialize the array; I build it on the fly.

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)

CodePudding user response:

It's because T is a list where all of the elements are the class int. Therefore, when you are comparing T[i] and T[i 1], one of them is a number, but the other one is still the int class. You should initialize T to an array of numbers, such as by doing [0] * 100.

Your problem with the infinite loop is caused in this section of code:

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

You need to change your condition to not T[i] > T[i-1]. That will fix both errors, because the program is referencing previously entered numbers in the condition, not the int class or an element that hasn't been assigned to.

  • Related