Reputation: 6891
I am having a look at python but I seem to have ran into a problem :
I have two arrays, one with every letter of a string I randomly got from a file and another array that is is filled with '_' for every letter in the word that the user needs to find.
However it seems that my while loop does not end, even when the word is found it keeps going.
Can anyone find my mistake ?
This is my first code in python, I come from java so tips and hinds on how to code better are also appreciated :)
thanks.
My code :
'''
Created on 12-jun.-2011
@author: k3r3nks7
'''
import random
class Hangman(object):
def _init_(self):
self.fileInput()
self.printStuff()
def fileInput(self):
wordList = open('wordlist')
woordenArray=[]
for line in wordList:
woordenArray.append(line)
i = random.randrange(0,len(woordenArray))
self.randomWoord = woordenArray[i]
def printStuff(self):
print 'I want to play a game\n\n'
print 'Hangman spelen ? (y/n)\n'
stringEval = raw_input()
self.checkToPlay(stringEval)
def checkToPlay(self,woord):
if woord == 'y':
print '#####################'
print '#let the game begin!#'
print '#####################'
self.iwanttoplayagame()
elif woord == 'n':
print 'have a nice day'
exit()
else:
print 'Your answer was not correct, please answer a correct parameter,\n resistance is futile, \n We are Borg sheep and .... \n ... oh look fresh gras \n'
self.printStuff()
def iwanttoplayagame(self):
incorrectGuesses = 0
self.correctArray = []
for char in self.randomWoord:
self.correctArray.append('_')
test=""
while(incorrectGuesses <= 6 or self.correctArray != self.randomWoord ):
print 'u raadde al : '+test.join(self.correctArray)
print "Geef is een letter \n"
letterInput = raw_input()
_c=0
if letterInput in self.randomWoord:
for letter in self.randomWoord:
if(letter == letterInput):
self.moveChar(letter,_c)
_c+=1
else:
incorrectGuesses+=1
self.display_figure(incorrectGuesses)
def moveChar(self,letter,c):
self.correctArray[c] = letter
def display_figure(self,bad_guesses):
graphics = [
"""
+--------+
|
|
|
|
|
|
====================
""",
"""
+-------
| o
|
|
|
|
====================
""",
"""
+-------
| o
| ---+---
|
|
|
======================
""",
"""
+-------
| o
| ---+---
| |
|
|
|
=====================
""",
"""
+-------
|
| o
| ---+
| |
| /
| /
|
|
|
|
=====================
""",
"""
+-------
| o
| ---+---
|
| /
| /
| /
|
=====================
""",
"""
+-------
| o
| ---+---
|
| / \
| / \
| / \
|
=====================
"""
,]
if(bad_guesses== 7):
print "you lost the game. The correct answer was : "+self.randomWoord
print "\n Do you want to play again ?"
inputS = raw_input()
self.checkToPlay(inputS)
print graphics[bad_guesses]
f = Hangman()
f._init_()
Upvotes: 0
Views: 863
Reputation: 70059
First of all in the fileInput() method the words read from the file will have '\n' in the end (i suspect this is why your loop don't end) to solve this you should do:
for line in wordList:
woordenArray.append(line.strip())
And you forgot to close your file 'wordlist', i recommend using a list comprehensible with the with statement
to populate the wordList
like this:
with open('wordlist') as wordList:
woordenArray = [line.strip() for line in wordList]
And i believe this loop :
_c=0
if letterInput in self.randomWoord:
for letter in self.randomWoord:
if(letter == letterInput):
self.moveChar(letter,_c)
Can be better written like this:
if letterInput in self.randomWoord:
for i, letter in enumerate(self.randomWoord):
if letter == letterInput:
self.correctArray[i] = letter
This also :
self.correctArray = []
for char in self.randomWoord:
self.correctArray.append('_')
Can be replaced by :
self.correctArray = ['_'] * len(self.randomWoord)
And you also misspelled the python __init__()
by written it _init_()
you should do:
class Hangman(object):
def __init__(self):
self.fileInput()
self.printStuff()
And you will not have to add f._init_()
in the end of your script.
EDIT:
One more think that i miss the while loop condition should be written like this:
while incorrectGuesses <= 6 and self.correctArray != list(self.randomWoord):
...
Before we were comparing self.randomWoord
which is a string with self.correctArray
which is a list, and also it should be and not or in the condition.
Hope this was helpful :)
Upvotes: 1
Reputation: 177991
In this line:
while(incorrectGuesses <= 6 or self.correctArray != self.randomWoord ):
or
should be and
. Also, correctArray
is a list, but randomWord
is a string, so they will never compare equal. Parentheses are unnecessary as well. Try this:
while incorrectGuesses <= 6 and ''.join(self.correctArray) != self.randomWoord :
_init_
should be __init__
, then you won't have to call it explicitly.
Lines read from wordlist will have a \n
appended. Use strip()
to remove it or your correctArray
will be one character too long.
for line in wordList:
woordenArray.append(line.strip())
Upvotes: 3