[Tutor] syntax error

Christopher Spears cspears2002 at yahoo.com
Tue Aug 1 00:29:13 CEST 2006


My brain has gone squishy.  I am combining a linked
list with a priority queue.  This is the last exercise
out of How To Think Like A Computer Scientist (Chapter
19).


class Node:
	def __init__(self, cargo=None, next=None):
		self.cargo = cargo
		self.next = next
		
	def __str__(self):
		return str(self.cargo)

class ImprovedQueue:
	def __init__(self):
		self.length = 0
		self.head   = None
		self.last   = None

	def isEmpty(self):
		return (self.length == 0) 
    
	def insert(self, cargo):
		node = Node(cargo)
		node.next = None
		if self.length == 0:
			# if list is empty, the new node is head and last
			self.head = self.last = node
		elif node.cargo > self.head.cargo:
			node.next = self.head
			self.head = node
		else node.cargo <= self.head.cargo:
			self.last.next = node
			self.last = node
		self.length = self.length + 1
		
	
	def remove(self):
    		cargo = self.head.cargo
    		self.head = self.head.next
    		self.length = self.length - 1
    		if self.length == 0:
			self.last = None
		return cargo

Here is the error message:
>>> from linkedPriorityQueue import *
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "linkedPriorityQueue.py", line 27
    else node.cargo <= self.head.cargo:
            ^
SyntaxError: invalid syntax

I am not sure what this means.  Everything is spelled
correctly.

-Chris
 



More information about the Tutor mailing list