I really need help with this...i'm trying to make a game that where user guesses a word and it tells the user
1: the right number of letter in the right place
2: the number of letter that is right in the wrong place

but when i test it out,

when the secret word is myrrh
my guess: yrmhr

it will say :
3 correct letter(s) in the wrong place
0 correct letter(s) in the correct place
(which is wrong)

======Heres the code=====

``````import randomWord

def replaceAt(string, index, character):
return string[:index] + character + string[index+1:]

def getSecretWord():
wordLength = str(raw_input("\nEnter length of word (4 to 13): "))
while not (wordLength.isdigit()) or not int(wordLength)>3 or not int(wordLength)<14:
print "That is not a number between 4 to 13!!"
wordLength = str(raw_input("Enter length of word (4 to 13): "))
print "\n"
secretWord = randomWord.getRandomWord()
wordLength= int(wordLength)
while len(secretWord) != wordLength:
secretWord=randomWord.getRandomWord()
return secretWord

def creatHyphenString(secretWord):
hyphenString = ""
for i in range(len(secretWord)):
hyphenString = hyphenString + "-"
return hyphenString

score = 0

while answer == "y": # will continue as long as the user want to play again
secretWord = getSecretWord()
secretWordLength = len(secretWord)

print secretWord

hyphenString = creatHyphenString(secretWord)
print hyphenString

wrongGuesses = 0

a = False
while a == False and wrongGuesses < 10:
guess = raw_input ("\nMake a guess: ")
guessLength = len(guess)
while not len(guess) == len(secretWord): # will run as long as its not same length
print "That is not the same length of the secret word!"
guess = raw_input ("\nMake a guess again: ")

##### need check alpha!!

countrightplace = 0
countwrongplace = 0
copysecretWord=secretWord
copyguess = guess

if str(copyguess)!=str(copysecretWord):
for i in range(len(copyguess)): # = 01234
if (copyguess[i] == copysecretWord[i]):
countrightplace = countrightplace+1
copysecretWord = copysecretWord[:i]+ "-" + copysecretWord[i+1:]
copyguess = copyguess[:i] + " " + copyguess[i+1:]

print copyguess
print copysecretWord

for i in range(len(copyguess)):
f = copysecretWord.find(copyguess[i])
if f >= 0:
countwrongplace = countwrongplace+1
copysecretWord = copysecretWord[:f]+ "-" + copysecretWord[f+1:]
copyguess = copyguess[:f] + " " + copyguess[f+1:]

print copyguess
print copysecretWord

print "\n", countwrongplace, "correct letter(s) in the wrong place"
print countrightplace, "correct letter(s) in the correct place"
wrongGuesses = wrongGuesses+1
print "You have", 10-wrongGuesses, "guesses remaining."

else:
a=True

if guess == secretWord:
print "\nYou win!"
score = score + 1
print "Your total score is:", score, "!"
else:
print "\nYou lose."
print "The secret word was: ", secretWord
print "Your total score is:", score ,"!"

answer = raw_input ("\nPlay again? (y/n): ")``````

I also tried to add the part where it checks user input if its all letters.. but it keeps erroring when i add that

Here is my solution of finding right place guesses and wrong place guesses, result contains '*' for correct place letters and '-' for wrong place letters, other letters are from secret word:

``````def check(guess,word):
result=['*' if c1==c2 else c2 for c1,c2 in zip(guess, word)]
for index, char …``````

Lists work better IMHO because you can alter each element (letter) individually. This is an example of how you could code a solution using lists. Hopefully some print statements will be enough to append the hyphenated string portion of your code. If not post back with questions. There are some …

woee's check is unfortunately buggy, check with my list function:

``````Secret: abcabc
Guess: bcacca
tonyjv check: ['-', '-', '-', '-', 'c', '-']
6  correct letter(s) in the wrong place
0  correct letter(s) in the correct place
0  incorrect letter(s) guessed``````

For me it seems like it's working.

guessAlpha is the function itself, you should call the function instead.

## All 19 Replies

Here is my solution of finding right place guesses and wrong place guesses, result contains '*' for correct place letters and '-' for wrong place letters, other letters are from secret word:

``````def check(guess,word):
result=['*' if c1==c2 else c2 for c1,c2 in zip(guess, word)]
for index, char in enumerate(guess):
if result[index] !='*':
if char in result:
result[result.index(char)]='-'

return result # * for correct place - for wrong place``````

Lists work better IMHO because you can alter each element (letter) individually. This is an example of how you could code a solution using lists. Hopefully some print statements will be enough to append the hyphenated string portion of your code. If not post back with questions. There are some flaws in the logic of the following code, because it is a simple example, but the general idea is what you want.

``````secret_word = "myrrh"
guessed_word = "mryrr"

secret_list = list(secret_word)
guessed_list = list(guessed_word)

count_right_place = 0
count_wrong_place = 0
incorrect_letter = 0
for ctr in range(0, len(secret_list)):
guess_ltr = guessed_list[ctr]

if guess_ltr in secret_list:
## if in correct place
if guess_ltr == secret_list[ctr]:
count_right_place += 1
secret_list[ctr] = "*"       ## eliminate this letter
else:
count_wrong_place += 1

else:
incorrect_letter += 1

print "%d  correct letter(s) in the wrong place" % (count_wrong_place)
print "%d  correct letter(s) in the correct place" % (count_right_place)
print "%d  incorrect letter(s) guessed" % (incorrect_letter)``````

I also tried to add the part where it checks user input if its all letters.. but it keeps erroring when i add that

"Error" is not a verb. And you'll have to post some code for help on that.

woee's check is unfortunately buggy, check with my list function:

``````Secret: abcabc
Guess: bcacca
tonyjv check: ['-', '-', '-', '-', 'c', '-']
6  correct letter(s) in the wrong place
0  correct letter(s) in the correct place
0  incorrect letter(s) guessed``````

For me it seems like it's working.

It does not take out the correct answer out before considering wrong place answers. Prove secret word 'abcd', guess 'aaaa'.

``````Secret:  abcd
Guess: aaaa
tonyjv check: ['*', 'a', 'a', 'a']
3  correct letter(s) in the wrong place
1  correct letter(s) in the correct place
0  incorrect letter(s) guessed
Guess:``````

I have correct answer one letter in right place i.e. one star.

This MasterMind version looks though overly difficult compared with standard color version. Maybe when you know category of word, it is OK.

thanks soo much for helping me!! I've been working on this for ages.. Thanks a lot!!

actually now i have another problem for checking if the input is alpha or not>.<

i added this at the top:

``````def guessAlpha(guess):
if guess.isalpha():
return True
else:
return False``````

and edit the while loop:

``````while not len(guess) == len(secretWord) and guessAlpha== True:
# will run as long as its not same length
print "That is not the same length of the secret word!"
guess = raw_input ("\nMake a guess again: ")``````

when i input numbers, it will still loop through it as letters.. i'm not sure why >.<

guessAlpha is the function itself, you should call the function instead.

commented: =) thanks for the help +0

lol somehow i got it working xD thanks a lot for the help =D

``````while not len(guess) == len(secretWord) and guessAlpha== True:
# will run as long as its not same length
print "That is not the same length of the secret word!"
guess = raw_input ("\nMake a guess again: ")``````

This will exit the loop if either of the conditions evaluates False. So you can have a guess that is not alpha, but if len(guess) == len(secretWord) you will still exit the while loop. Instead of checking for false entries, exit the loop only if the entry is "True", i.e. meets all of the conditions. The following code will not print both messages for a wrong length that is also a non-alpha entry, but you can decide if you want more detailed messages or not.

``````while True:
if len(guess) == len(secretWord):
if guess.isalpha():
break     ## exit while loop
else:
print "Enter letters only"
else:
print "That is not the length of the secret word!"``````

I do have my code for your problem, if you want to post it after you have cleaned up your solution to suitable condition, announce here. I do not say it is correct answer as there are as many styles to program as programmers but I do see many points to improve in your coding, especially your coding of getSecretWord.

(My thousandth post :) )

Hi! Thank you so much for the replies! I been doing some finishing touch on this program, trying to organize the code into functions.. however i'm stuck here again >.<

My problem right now is... the while loop and the if loop ..

I dont know how to return more then one variable therefore, i'm totally stuck there...my teacher wants me to organizet he whole code (most of it) into fuctions. Pleaseee help me. I'm pretty new with python. Thanks a lot!!

``````while a == False and wrongGuesses < 10:

zz = False
ss = False
while zz == False or ss== False:
guess = raw_input ("\nMake a guess: ")
guessLength = len(guess)

if guessAlpha(guess) == True:
zz=True
else:
print "input not a word!"
zz=False

if len(guess) == len(secretWord):
ss = True
else:
print "input length not the same as the secret word!"
ss=False``````

and

``````if str(copyguess)!=str(copysecretWord):
for i in range(len(copyguess)):
if (copyguess[i] == copysecretWord[i]):
countrightplace = countrightplace+1
copysecretWord = copysecretWord[:i]+ "-" + copysecretWord[i+1:]
copyguess = copyguess[:i] + " " + copyguess[i+1:]

for f in range(len(copyguess)):
if copyguess[f] in copysecretWord:
f = copysecretWord.find(copyguess[f])
if f >= 0:
copysecretWord = copysecretWord[:f]+ "-" + copysecretWord[f+1:]
countwrongplace = countwrongplace+1

print "\n\t", countwrongplace, "correct letter(s) in the wrong place"
print "\t", countrightplace, "correct letter(s) in the correct place"
wrongGuesses = wrongGuesses+1
print "\n\tYou have", 10-wrongGuesses, "guesses remaining."

else:
a=True``````

I've been working on this problem for a few days now.. Lol... it would be great if someone can help me with this >.< i really need to finish this asap T_T THANKSS!!

Checking in function as I did it.
Another function to process one game.
Main routine the while loop asking user if he wants to continue to play

how can i make the while loop into a function?

``````def function(parameter):
while True:
## do stuff
if time_to_finish: return
## do more stuff``````

I would myself maybe keep the main loop in main routine. It could be put in another similar function though if you or youir teacher prefer (main function like in some other languages and just main() in execution part of the program).

would it make sense if i type the following:

``````def checkLengthAlpha(secretWord):
while a == False and wrongGuesses < 10:
zz = False
ss = False
while zz == False or ss== False:
guess = raw_input ("\nMake a guess: ")
guessLength = len(guess)

if guessAlpha(guess) == True:
zz=True
else:
print "input not a word!"
zz=False

if len(guess) == len(secretWord):
ss = True
else:
print "input length not the same as the secret word!"
ss=False
return guess, guessLength, wrongGuesses, a``````

then i insert

``````guess, guessLength, wrongGuesses, a = \
checkLengthAlpha(secretWord)``````

to where i need it?

Would u mind to post the hole code to see if it works.... Its becouse im a beginer, so i got confused to see all the corrections...
I would be really haooy if anyone of u could post the correct and final code PLEASE!!!!!

Hi im a noobie in python... and i want to do a master mind game
Can u post the hole code plz...
Its becouse i dont understand ver well all the corrections u made

PLz i need this help