For problem set 3, for the first function, I try to do the following: global word_list assert word in word_list And then, I call the function later at the bottom of the code like so: get_word_score("hello",7) I get an assertion error! Is hello not in word_list? What is happening?
Sorry, I know that hello is in word_list. What should I do?
post your code using a code pasting site like http://dpaste.com or http://pastebin.com
Did you change the variable name? I think in that code, the variable name is wordlist, not word_list
No, that is the formal parameter of the function while it is defined. word_list was set equal to the function, so it's called word_list. Here is the full function I wrote: http://dpaste.com/763841/ If I remove the assert, everything else works.
did you try it in the shell (IDLE?)? 'hello' in word_list? it should return True works for me, maybe load isn't working - http://dpaste.com/763890/
Take note that all the words in the words.txt file are in uppercase so check if load returns a list with uppercase or lowercase words. If it is the former case then any word you search with lowercase would never return True for "word in word_list"
No, that's not it. I tried: assert word.upper() in word_list but that did not work. How do you open it in IDLE? The file has to be in the same folder as the word list.
are you running it in a shell (like Idle?) - if so, the functions and the variable word_list should be available in the shell and you can enter statements in the shell to see what the results are, like what i did in my post above.
Are you sure that your word list is not empty?
I tried it in IDLE, same results, and I've opened the word file, and see one word on every line.
It seems to be complete, and when I use ctrl-F, I can find hello.
what happens when you enter 'hello' in word_list - in Idle?
I wasn't referring to the file. I meant did you check if your word_list variable is empty or not.
When I put 'hello' in word_list, in IDLE, I get False. When I printed out wordlist, I found something like this: [b'aa', b'aah', b'aahed', b'aahing', b'aahs', b'aal', ... , b'zyzzyvas', b'zzz'] I don't think everything should have a b before it.
hmmm, when you look at the list with a text editor are the b's there? sometimes when you see things like that it is saying that the string is encoded did you write load words or was it provided? - post your load words - does it strip everything but the text from the line? here's the one from my file - http://dpaste.com/765125/
Yes, my load words is original, from the problem set that is. The b's are not there when I simply look at the file with a text editor. Here is the function: http://dpaste.com/765147/
By the way, I use python 3.2
That may have been important.
Oh I see... I think python 3.x uses b as a prefix in front of string. Are you using the 2011 SC course? If yes, please use an older version of Python since it is specifically said in the course page that the codes used there are NOT compatible with Python 3.x. Please use Python 2.5 (2.6 or 2.7) will also do. Also when debugging, try to discover the bugs in your code by using print statements.
do you have something like this at the top of your py file? # -*- coding: <encoding-name> -*- so the b'xyz' is a string literal 2.7 docs: http://docs.python.org/reference/lexical_analysis.html#string-literals 3.2 docs: http://docs.python.org/py3k/reference/lexical_analysis.html#string-and-bytes-literals hmmm in line ten of your load_words, change the 'rb' to just 'r' or 'rt' and see what happens http://docs.python.org/py3k/library/functions.html#open
The 'rb' has to stay, or else the input/output becomes unbuffered. I don't have that line of code at the top of my py file. Is there any way to do something similar to opening the text file in 3.2?
you aren't using buffering - it is turned off. and you are reading a text file so why not open it as text instead of binary?
Whenever I change the 'rb' to anything else, I get an error message which says I can't have unbuffered text I/O. And how am I opening it as a binary? I'm only using the open() command.
I fixed it.
I simply replaced inFile = open(WORDLIST_FILENAME, 'rb', 0) with inFile = open(WORDLIST_FILENAME, buffering=-1)
passing 'rb' as the mode parameter the b means binary try open(WORDLIST_FILENAME, 'r', 0) or open(WORDLIST_FILENAME) http://docs.python.org/py3k/library/functions.html#open
yeay
Thanks
Join our real-time social learning platform and learn together with your friends!