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
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
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 letter is not found, no need to go further
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.
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
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
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
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.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
guessAlpha is the function itself, you should call the function instead.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
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!"
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
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 :) )
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
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
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
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).
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
Post your code and any errors to new thread. Include link to this thread and explain what you coded yourself
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852