Comparing element in a list

Question!

I just started learning Python... I am writing a simple program that will take in integers and keep an unsorted and sorted list. I'm having a problem with the sorted list part... I'm getting an error when comparing the values of elements within the list. Where I get the error is the following line: "if sortedList[sortcount] > sortedList[count]:". I get "TypeError: unorderable types: list() > int()".

Here is part of the code... I'm not sure what is wrong.

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


Answers

You should consider using:

sorted(numberList)

to generate your sorted list. Much, much simpler not having to reinvent the wheel.

Example:

>>>unSorted = [3, 4, 1, 5]
>>>unSorted
[3, 4, 1, 5]
>>>sortedList = sorted(unSorted)
>>>sortedList
[1, 3, 4, 5]


This video can help you solving your question :)
By: admin