Member Avatar for leegeorg07

hi im making a chat program and im having a spot of trouble with a small part of code:

#def_unknown
word_list=open("words.txt", "a")
never_save=open("banned.txt", "a")
def define_word(word):
    if word not in never_save.readlines()
        question="do you want to save %s? (y=yes n=not this time nn=no never)"% word
        define = raw_input(question)
        if define.lower()=="y":
            word=(word+' ')
            word_list.write(word)
        elif define.lower()=="n":
            pass
        elif define.lower()=="nn":
            never_save.write(word)

it says that the "not in" is bad syntax. What can i do to fix it?

oh and i know it tabbed in but i was using the idle to make a quick draft and then finish it neatly afterwards.

if word not in never_save.readlines():
You need : or SyntaxError.

Member Avatar for leegeorg07

oh really i thought i put that in, ill try it

Member Avatar for leegeorg07

it now says that "not in" is a bad descriptor, what is the best fix to that?

"bad descriptor" refers to a file. To use readlines, you should open the file in "r" mode. Also another problem is that after the first call, the file position will be at the end of the file, so you can't call your function twice. A solution is to add a line never_save.seek(0) before the if block, to go back to the beginning of the file.

Please post the error message to avoid this type of "looking for 'not in' as bad syntax when it is really a colon" errors. Same thing for "not in" is a bad descriptor. The error message probably has other info which possibly points to "never_save.readlines()" as the real problem.

Member Avatar for leegeorg07

ok thanks, what is the best way for me to have that and still be able to append to it along with the "a" mode?

f = open("banned.txt", "r+w") . Then f.seek(0) to go to the beginning of the file and f.seek(0, 2) to go to the end of the file (before you use f.write to append something).

Member Avatar for leegeorg07

thanks that got rid of the error, and it works but now it wants to save the words everytime :( what can i do? i know i sound quite noobish but i have no idea

thanks that got rid of the error, and it works but now it wants to save the words everytime :( what can i do? i know i sound quite noobish but i have no idea

Post your newest code.

Member Avatar for leegeorg07

ok this is the def unkown:

#def_unknown
word_list=open("words.txt", "a")
never_save=open("banned.txt", "r+")
def define_word(word):
    if word not in never_save.readlines():
        question="do you want to save %s? (y=yes n=not this time nn=no never)"% word
        define = raw_input(question)
        if define.lower()=="y":
            word=(word+' ')
            word_list.write(word)
        elif define.lower()=="n":
            pass
        elif define.lower()=="nn":
            never_save.write(word)
def word(word):
     print("do you wanna save" + word)
     print("y=yes n=not this time nn=never")
     define = input("Enter choice: ")
     if define.lower() not in never and define.lower() == "nn":
	never.append(word)
     elif define.lower() == "y":
	word = word + " "
	words.append(word)
     elif define.lower() == "n":
	pass
     else:
	print("invalid choice")

I tried this in idle and the out put was.

>>> word("arg")
do you wanna savearg
y=yes n=not this time nn=never
Enter choice: y
>>> print(words)
['thing', 'stuff', 'morestuff', 'arg ']
>>> word("noooo")
do you wanna savenoooo
y=yes n=not this time nn=never
Enter choice: n
>>> print(words, never)
['thing', 'stuff', 'morestuff', 'arg '] ['no', 'notta', 'never']
>>> word("noooo")
do you wanna savenoooo
y=yes n=not this time nn=never
Enter choice: nn
>>> print(words, never)
['thing', 'stuff', 'morestuff', 'arg '] ['no', 'notta', 'never', 'noooo']
>>>
Member Avatar for leegeorg07

thanks, but after changing my code to your suggestions it still does it, i have attached a zip with the files in

def word(word):
     print("do you wanna save" + word)
     print("y=yes n=not this time nn=never")
     define = input("Enter choice: ")

## if define.lower() not in never and define.lower() == "nn":
##     never.append(word)
     if word.lower() not in never and define.lower() == "nn":
         never.append( word.lower() )

     elif define.lower() == "y":
	word = word + " "
	words.append(word)

     elif define.lower() == "n":
	pass
     else:
	print("invalid choice")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.