I've made the game Mastercode and I am having troubles getting the computer to tell the user which numbers they got correct and incorrect.

My code is listed below, along with the attempt I used for getting the computer to print the correct answers. If someone could tell me what I am doing wrong and point me in the right direction, that would be great.

``import randomdef masterMind():Password = "%05d" % random.randint(0, 99999) #the computer chooses 5 random numbersfor tries in range(10):userGuess = raw_input("Guess my 5 digit password to access the treasure:")if Password == userGuess:print "Win on the %d try" % (tries + 1)hint(password, userGuess)break #terminates the ongoing loop and executes next statementprint "answer was:", Password #tells computer to print the passworddef hint(password, guess): #function of the hintsfor i in range(5): #the range within the five integersif guess[i] == password[i]: #if the user's integer aligns with computers integer then an 'x' should appearprint 'x',continueif guess[i] in answer: #if don't have corresponding number then an 'o' will appearprint 'o',``

About the masterMind() part : The hint() call is executed at the wrong place. Also it uses `password` and not `Password` as a parameter

``````def masterMind():
Password = "%05d" % random.randint(0, 99999) #the computer chooses 5 random numbers
for tries in range(10):
userGuess = raw_input("Guess my 5 digit password to access the treasure:")
print "Win on the %d try" % (tries + 1)
break #terminates the ongoing loop and executes next statement
else :
``````

`answer` isn't defined anywhere, I think you meant `password`.

I'm not sure you can work with integers like that, but it'll be fine if you convert them to strings.

Finally the algorithm structure doesn't work well. If a number of the guess is at the right place, the guess function will both echo `x` and `o`.

You should use a `if ... elif` structure, and then you could add a `else` clause to notify the user that this number isn't in the password at all !

Try to rewrite your hint() function with these indications, but if you need more help, here's a working solution.

``````def hint(password, guess): #function of the hints
for i in range(5): #the range within the five integers
if str(guess)[i] == str(password)[i]: #if the user's integer aligns with computers integer then an 'x' should appear
print 'x',

elif str(guess)[i] in str(password): #if don't have corresponding number then an 'o' will appear
print 'o',

else:
print '_', # _ is displayed if the number isn't in the password at all.
``````

Also, it would be nice to check that the user's guess has exactly five digits. If it has less than five digits, there'll be an error.

I think you really need to check for black and white pegs in separate parts of hint(). This allows you to REMOVE something that "matched as black", and not [incorrectly] score additional whites for it.

Using lists, this can be implemented as such:

``````def hint(password, guess):

# convert the strings to lists so we can to assignments, below
guess_list = list(guess)

# check for black (correct number in correct position)
for i in range(5): #the range within the five integers
print 'x',
# punch in some non-possible value so we can exclude this on the check for white
guess_list[i] = None

# check for white (correct number in wrong position)
for i in range(5):
if guess_list[i] == None:
continue
print 'o',
# remove this from the password list, so that a given
# password digit doesn't incorrectly count as multiple white

# or this would work, too:

print
``````

One you have more Python experience you might find more concise ways to do this with set() objects or Counter() objects... but see if you can see WHY the above works.

Here's my test case: I set the password to "12345", then did these tests:

``````Guess my 5 digit password to access the treasure:11111
x
Guess my 5 digit password to access the treasure:21777
o o
Guess my 5 digit password to access the treasure:77721
o o
Guess my 5 digit password to access the treasure:21774
o o o
Guess my 5 digit password to access the treasure:21775
x o o
Guess my 5 digit password to access the treasure:14355
x x x o
Guess my 5 digit password to access the treasure:12345
Win on the 7 try
``````

Is that the result you are looking for?

First, you should move the `hint` call out of the `if` block (probably it's not a good idea to hint a user only when he/she got the password right):

``````if Password == userGuess:
print "Win on the %d try" % (tries + 1)
break #terminates the ongoing loop and executes next statement
``````

By the way you were calling `hint` as `hint(password, userGuess)`. Python names are case-sensitive and you should call it like this: `hint(Password, userGuess)`. Well, really you should rename the `Password` variable to `password` - common Python convention is to use lowercase in the variable names.

Second, you have undefined variables in the `hint` function. I think this function should look like this:

``````def hint(password, guess): #function of the hints
for i in range(5): #the range within the five integers
if guess[i] == password[i]: #if the user's integer aligns with computers integer then an 'x' should appear
print 'x',
else:
print 'o',
``````

With these changes your code works: I got `18744` password on the 10th try.

Top