5 Replies - 8078 Views - Last Post: 22 August 2012 - 06:52 PM Rate Topic: -----

#1 krielovas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 14-August 12

TypeError: unsupported operand type(s) for -: 'list' and '

Posted 14 August 2012 - 03:12 PM

def bubble(n):
    change = False
    while change == False:
        for p in range(n - 1):
            j1 = list[p]
            j2 = list[p + 1]
            if j1 > j2:
                list[p] = j1
                list[p + 1] = j2
            else:
                list[p] = j2
                list[p + 1] = j1
                change = true
    return(n)
import random
number = []
for a in range(20):
    number.append(random.randint(1, 100))
bubble(number)



Traceback (most recent call last):
File "C:\eclipse\workspace\bubble test 1\test 1", line 20, in <module>
bubble(number)
File "C:\eclipse\workspace\bubble test 1\test 1", line 5, in bubble
for p in range(n - 1):
TypeError: unsupported operand type(s) for -: 'list' and 'int'

Help me understand what I am doing wrong.

This post has been edited by atraub: 14 August 2012 - 07:45 PM
Reason for edit:: fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: TypeError: unsupported operand type(s) for -: 'list' and '

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,313
  • Joined: 21-June 11

Re: TypeError: unsupported operand type(s) for -: 'list' and '

Posted 14 August 2012 - 03:52 PM

number is a list containing 20 random numbers. You're giving that list as an argument to bubble. So when bubble runs, n is that same list of 20 numbers. Then on line 5 you write n - 1.

Generally when you use - both operands should be a number (or some other types that can be subtracted from each other). Here you're trying to subtract the number 1 from a list. As the error message is telling you, you can't do that.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,289
  • Joined: 18-April 07

Re: TypeError: unsupported operand type(s) for -: 'list' and '

Posted 14 August 2012 - 04:07 PM

Not to mention your algorithm is not correct. Everywhere you mention list[] it should be n[] and also if j1 > j2 why are you setting list[p] to j1? It should be set to j2 and list[p+1] should be j1.

Because the way you have it you are essentially doing list[p] = list[p].

Also you don't need the else in there. If j1 is greater than j2, switch them and go around for another pass.

Lastly, take a look at how you are using your boolean flag. I think you might be missing a crucial step in it.

:)
Was This Post Helpful? 1
  • +
  • -

#4 krielovas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 14-August 12

Re: TypeError: unsupported operand type(s) for -: 'list' and '

Posted 16 August 2012 - 04:22 PM

What I'm trying to do is make a bubble sort where p equals a number in the list and p + 1 equals the next number in the list.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,854
  • Joined: 16-October 07

Re: TypeError: unsupported operand type(s) for -: 'list' and '

Posted 16 August 2012 - 05:52 PM

You've been told rather clearly what's going on. However, I do like a good example.
>>> # list is defined
... number = [5, 2, 12, 8]
>>> number
[5, 2, 12, 8]
>>> # we give it an even stupider name
... n = number
>>> n
[5, 2, 12, 8]
>>> # see if this means anything
... n - 1
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: unsupported operand type(s) for -: 'list' and 'int'
>>> # clearly this won't
... range(n - 1)
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: unsupported operand type(s) for -: 'list' and 'int'
>>> # just for yucks, is this anything?
... list[0]
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: 'type' object is unsubscriptable
>>> # now, if it were this we might be on to something
... n[0]
5
>>> # this also has meaning
... len(n)
4
>>> 



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#6 krielovas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 14-August 12

Re: TypeError: unsupported operand type(s) for -: 'list' and '

Posted 22 August 2012 - 06:52 PM

Fixed

def bubble(n):
    finished = False
    while not finished:
        finished = True
        for p in range(0, len(n)-1):
            if n[p] < n[p+1]:
                left = n[p]
                right = n[p+1]
                n[p] = right
                n[p+1] = left
                finished = False
    print(n)
#
import random
number= []
for a in range(20):
    number.append(random.randint(1, 200))
#
print(number)
#
bubble(number)


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1