[Tutor] Re: Translating to Python [perl --> python]

alan.gauld@bt.com alan.gauld@bt.com
Tue, 2 Jul 2002 15:30:32 +0100


> import cgi
> 
> print "Content-type: text/html\n\n"
> 

try:    ####### need 'try' to get 'except' to work... ####
 	form = cgi.FieldStorage()
 	c = form['c'].value
except KeyError :
 	c = ""

Notice the indentation above, it is critical.

> content = c
> if c == 'abc':
> content = "abc123"

again you have no indentation, Python needs the 
indentation its not just a matter of style.

> elif c == 'def':
> content = "def456"
> else:
> print "error: content failed\n"
> 
> file = open("text.txt")
> text = file.read()
> file.close()
> 
> print """
> 
> %(content)s
> 
> 
> <br>&nbsp;<br>&nbsp;<br>&nbsp;
> 
> %(text)s
> 
> """ % vars()
> 
> 
> It says there is an error near except KeyError: but I've never used
> python before so I don't know what to change.

Thats the lack of try first and the lack of indentation second


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