This is my Code and it gives me this error message. Why?
JavaScript
x
20
20
1
if __name__ == '__main__':
2
l=[]
3
N = int(input())
4
for z in range(N):
5
x=input().split()
6
if (x[0]=="insert"):
7
l.insert(int(x[1]),int(x[2]))
8
elif(x[0]=="print"):
9
print(l)
10
elif(x[0]=="remove"):
11
l.remove(int(x[1]))
12
elif (x[0]=="append"):
13
l.append(x[1])
14
elif(x[0]=="sort"):
15
l.sort()
16
elif(x[0])=="pop":
17
l.pop()
18
elif(x[0]=="reverse"):
19
l.reverse()
20
Error Message – Traceback (most recent call last): File “/tmp/submission/20220617/03/45/hackerrank-3495035b4042c8bc0c55e799a2d2e687/code/Solution.py”, line 15, in l.sort() TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’
Advertisement
Answer
You appended string value at x[0] == "append"
.
It should work when you change to l.append(int(x[1])