[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Question - Comparing strings
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