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

Re: [pygame] Question - Comparing strings



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
>