[Tutor] Simpler way to do this (for-loop)?

Olli Rajala olli.s.rajala at tut.fi
Tue Nov 16 12:34:16 CET 2004


Okay, I've been coding my own photogallery and in admin-site I have
this kind of code. Well, to put it short, it works but is anything
but goodlooking... And I have no idea how to make it better, so I
thought to ask here.

So, 'categories' is just a list with names of categories and I'd like
to set the default value of the select in html the value found in
'category'. Sorry the not so good English. :( 

So, any ideas how to refactor that part of code?

************************************************************
print 'Category: <select name="category">'
if not category:
	print '<option selected>(Choose the category)</option>'
	for line in categories:
		print '<option>%s</option>\n' % (line)
else:
	print '<option>(Choose the category)</option>'
	for line in categories:
		if line == category:
			print '<option selected>%s</option>\n' % (line)
		else:
			print '<option>%s</option>\n' % (line)
print '</select><br />'
************************************************************

TIA, 
-- 
<><
"Quite normal guy"
Olli Rajala


More information about the Tutor mailing list