当前位置: 动力学知识库 > 问答 > 编程问答 >

python - MasterMind problems

问题描述:

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 random

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:")

if Password == userGuess:

print "Win on the %d try" % (tries + 1)

hint(password, userGuess)

break #terminates the ongoing loop and executes next statement

print "answer was:", Password #tells computer to print the password

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',

continue

if guess[i] in answer: #if don't have corresponding number then an 'o' will appear

print '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:") 
        if Password == userGuess: 
            print "Win on the %d try" % (tries + 1)                 
            break #terminates the ongoing loop and executes next statement
        else :
            hint(Password, userGuess) 
    print "answer was:", Password #tells computer to print the password

About the hint part :

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 
  password_list = list(password)
  guess_list = list(guess)

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

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

          # or this would work, too:
          #password_list[password_list.index(guess_list[i])] = None

  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
hint(Password, userGuess)

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: