我刚开始学习 Python...我正在编写一个简单的程序,它将接受整数并保留一个未排序和已排序的列表。我在排序列表部分有问题...... 比较列表中元素的值时出现错误。我得到错误的地方是以下行:“if sortedList[sortcount] > sortedList[count]:”。我得到“TypeError:不可排序的类型:list() > int()”。
这是部分代码...我不确定哪里出了问题。
numberList = []
sortedList = []
count = 0
sum = 0
....(skip)....
sortcount = 0
sortedList += [ int(userInput) ]
while sortcount < count:
if sortedList[sortcount] > sortedList[count]:
sortedList[count] = sortedList[sortcount]
sortedList[sortcount] = [ int(userInput) ]
sortcount+=1
最佳答案
你在哪里:
sortedList[sortcount] = [ int(userInput) ]
你应该这样做:
sortedList[sortcount] = int(userInput)
否则你会在该位置添加一个列表并给出你告诉的错误。
顺便说一句,while循环之前的第一行最好写
sortedList.append(int(userInput))
关于python - 比较列表中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8865509/