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

Re: [pygame] Question - Comparing strings



Okay, I admit it, I'm bored enough to look deeper.  Offhand, I see a
few things that don't fit right.

> 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.

> 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"):

> 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))

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.

-FM

On 9/4/08, Charlie Nolan <funnyman3595@xxxxxxxxx> wrote:
> Glad to see a new newbie, it's the only way to get experts.  On the
> other hand, this is the *pygame* mailing list.  We use python, but
> it's not exactly the topic at hand.  For more general discussion, try
> one of the places listed here:
> http://python.org/community/
>
>
> That said, I'm a nice guy, so you get a free pass this time.  It
> sounds like you've probably got whitespace hanging around that you
> don't want.  Try changing this:
> print "the word was", word, "and you guessed", guess
> to this:
> print "the word was %r and you guessed %r" % (word, guess)
>
> My guess is that you'll get something like this printed out:
> the word was 'cheese\n' and you guessed 'cheese'
>
> In that case, just add .strip() to the end of whichever line you set
> that variable on, so in my example, it'd change this:
> word = WordSelection()
> to this:
> word = WordSelection().strip()
>
> -FM
>
> On 9/4/08, escapedpolarbear@xxxxxxxxxxxxxx
> <escapedpolarbear@xxxxxxxxxxxxxx> wrote:
>> Hey,
>> I only recently found out about Python and started looking at it a
>> week ago. It seems like a brilliant coding language.
>>
>>  I'm starting by making a small simple "guess the word" (hangman
>> clone) type game. The game selects a random word from a .txt file
>> (with the option to either use the standard txt file or enter your
>> own).
>>
>> The two problems I have is:
>> 1. When I compare the word the person guesses and the word the program
>> chose, it treats it as the "wrong word" no matter what.
>>
>> 2. When I try puttin a number on the first line of the file (to be
>> used in the random number generator so that it cuts the range down to
>> between 2 and number-of-lines-in-file) I get: "TypeError: cannot
>> concatenate 'str' and 'int' objects"
>>
>> I'm completely new at this, and I can't seem to find a sollution in
>> any of the documentation or tutorial pages...
>>
>> the code:
>> import random     # Imports lib for random function
>> import linecache  # Imports lib for reading one line from a file
>>
>> def WordSelection():
>> 	WordListFile = raw_input("enter word list file name, or choose the
>> (d)efault list: ")
>> 	if WordListFile == "default" or "default" or "Default" or "d" or "D":
>> 		WordListFile = "WordList.txt"
>> 	MaxLine = linecache.getline(WordListFile, 1)
>> 	GottenWord = linecache.getline(WordListFile, random.randint(2,
>> MaxLine))
>> 	return GottenWord
>>
>> print "Welcome to Guess The Word"
>> print "v0.1b by: Esc"
>> word = WordSelection()
>> print "the word is: ", word
>> guess = raw_input()
>> print "the word was", word, "and you guessed", guess
>>
>> if guess == word:
>> 	print "well done, that was correct."
>> else:
>> 	print "to bad, that was incorrect."
>>
>>
>> The .txt file:
>> 5                                                 <- the highest
>> random number
>> cheese                                   \
>> apple                                        } The words that the
>> program reads
>> pear                                          } and uses in the game.
>> nut                                           /
>>
>> When I remove MaxLine and put in 4 instead it works like a charm.
>>
>> hope someone can help,
>>     - esc
>>
>