[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Question - Comparing strings



On Thu, 4 Sep 2008 08:29:51 -0500, "Charlie Nolan" <funnyman3595@xxxxxxxxx>
wrote:
>> def WordSelection():
> 
> In Python, we write function names like_this.  CamelCase is for
> classes, UPPERCASE for constants.  Everything else gets
> lowercase_with_underscores.  It's just a style convention, but it
> makes it _much_ easier for others to read your code.  I'll make the
> switch when I rewrite a line, just be sure to change things uniformly.

On a dissenting note, I'm one of those deviants who names functions with
CamelCase, on the theory that lc_with_underscores confuses functions and
variables.


>> if WordListFile == "default" or "default" or "Default" or "d" or "D":
> 
> This does not do what you think it does.  What you want is this:
> if word_list_file.lower() in ("default", "d"):

By way of further explanation, what Python is doing with the right side of
the original line is selecting the first item that evaulates to True. Any
string other than "" is True, so the result is "default". So, the line
works out to 'if WorldListFile == "default"', and the effect would be that
the other options like "D" won't count as recognized alternatives.


>> MaxLine = linecache.getline(WordListFile, 1)
>> GottenWord = linecache.getline(WordListFile, random.randint(2, MaxLine))
> 
> You've got a type problem here.  Python doesn't require you to declare
> what type any variable is, but it does rigidly enforce them.
> Essentially, what's happening is that somewhere in random, a bit of
> code will try to do this:
> "4" + 1
> 
> Since there are two logical ways to put these together, "41" and 5,
> Python just disallows adding strings and ints together.  What you need
> to do is convert, like so:
> max_line = int(linecache.getline(word_list_file, 1))

When you load data from a text file, it's all going to be in the string
format unless you specify otherwise. 4 becomes "4". Use the functions int()
and str() to convert.


> You're also getting your lines the Hard Way(tm).  It's much easier to
> leave out the line count and do this:
> lines = open(file_name).readlines()
> chosen_word = random.choice(lines).strip()
> 
> When you go a step further and allow multiple words, you should make
> sure that you grab the lines before you enter the loop, so that you
> only fetch them once.

I wouldn't bother with the "import linecache" at all. Nolan's method looks
a little simpler than what I'd normally do here:

f = open(file_name)
text = f.read() ## Gets the contents of the file
f.close() ## Windows locks the file if the program ends/crashes with this
unclosed
lines = text.split("\n") ## Creates a list from the lines, eg. ["line
0","line 1","line 2"]

(You can even split the text by paragraph breaks -- "\n\n" -- and then by
line breaks. I've done that to create an improvised config file format.)

Then you can do something special like convert line 0 to an integer to
treat it as some kind of configuration option, but if what you're using it
for is to know how many lines there are, all you need to do is say,
"len(lines)". You can pick a random line with "line =
lines[random.randint(0,len(lines)-1)]". Weirdly, randint() is inclusive,
hence the -1 in there, while range() isn't.