[Tutor] (no subject)

alan.gauld@bt.com alan.gauld@bt.com
Fri, 26 Jul 2002 12:20:00 +0100


> This is the error I get when I try to execute my script:
> Traceback (most recent call last):
>   File "/home/sites/sbn/www/public_html/index.py", line 671, in ?
>     print """
> KeyError: content


You have a try/except with this:
try:
   ....
except KeyError:
   con = ""
   content = con

   if con == "index": 
      #very long if/elif chain here

But this can never be true since you are still 
inside the exception handler(indentation) and 
you have just set con = "" not "index"...

I suspect you need to check the indentation of 
the if statement?

Otherwise content only gets set if a Keyerror occurs
and when you try to print it at the end you get the error...

Also you might find it easier to maintain to put all 
the HTML stuff in separate files and then read it in.

Something like:

if con == "index":
   content = open("index.htm").read()
elif con == "index2):
   content = open("index2.htm").read()
elif....

That splits the maintenance of the HTML/Javacript 
away from maintaining the Python code.

Just a thought,

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld