[Tutor] Checking for string in a list strangeness

AdamC kabads at gmail.com
Mon Apr 6 09:30:41 CEST 2009


I'm writing a small cgi application for children to use and I want to
check that the name they enter isn't a swear word.

I've populated a database with a swear word list and then check this
list against the input, but it always shows up as not being a swear
word.

Can anyone see why this would always throw up FALSE, even if I supply
a word which is in the list?
TIA
Adam

Code (with some of my debugging - so this is not the finished article).

#!/usr/bin/env python
from random import *
from cgi import *
import cgitb; cgitb.enable()
import string
import MySQLdb
print "Content-type: text/html\n\n"
try:
    connection =
MySQLdb.connect(host="learningtimes.db",user="adcripps",
passwd="ed3oq3zu", db="times")
except MySQLdb.Error, e:
    print "Error %d %s" %(e.args[0], e.args[1])
    sys.exit(1)
mycursor = connection.cursor()
print """<h1>Join the game</h1>"""

print """<form action = "join.py" method=get>
            <input type = text name = "team" />
            <input type = "Submit" value="Join" />
            """
#Popluate my swear word list from the db.
mycursor.execute("select badword from badwords")
myrowcount = int(mycursor.rowcount)
swearlist = []
for i in range (0, myrowcount):
    myrow = mycursor.fetchone()
    swearlist.append(str(myrow))

team = FieldStorage()

#Need some kind of swear checker here.

#for i in swearlist:       # shows swear list OK
#    print i;

checkteam = team.getvalue('team')
#print """<p>"""+str (checkteam) + """</p>""";           # this works OK
#print str(swearlist)
    # As does this

print "name = " + str(team.getvalue('team'))
if str(team.getvalue('team')) in swearlist:                 # This is fail!
    print "swearing"
else:
    print "not swearing"
if team.getvalue('team'):
    if team.getvalue('team') in swearlist:                    #This
also is fail.
        print "swear"
        sys.exit(0)
    else:
        print "no swear"
        team = team.getvalue('team')
        statement = """insert into pupil (nickname, points, played,
morepoints, moreplayed, footballteam) values ('""" + team + """',
0,0,0,0,0)"""
        mycursor.execute(statement)
        print """<br />You have now joined.<br /><a
href="/times/football/football.py">You should now support a
team.</a>"""

# disconnect the database
mycursor.close()
connection.close()

--
You back your data up on the same planet?
http://www.monkeez.org
PGP key: 0x7111B833


More information about the Tutor mailing list