MasterMind问题

时间:2014-01-31 13:34:11

标签: python

我制作了游戏Mastercode,我遇到麻烦让计算机告诉用户他们哪些数字是正确和错误的。 下面列出了我的代码,以及我用于让计算机打印正确答案的尝试。如果有人能告诉我我做错了什么并指出我正确的方向,那就太好了。

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

3 个答案:

答案 0 :(得分:0)

首先,您应该将hint调用移出if块(可能仅在他/她正确获取密码时提示用户不是一个好主意):

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

顺便说一下,您将hint称为hint(password, userGuess)。 Python名称区分大小写,您应该这样称呼它:hint(Password, userGuess)。嗯,实际上你应该将Password变量重命名为password - 常见的Python约定是在变量名中使用小写。

其次,hint函数中有未定义的变量。我认为这个函数应该是这样的:

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

通过这些更改,您的代码可以正常工作:我在第10次尝试时获得了18744密码。

答案 1 :(得分:0)

我认为你真的需要在提示()的不同部分检查黑色和白色的钉子。这允许您删除“匹配为黑色”的内容,而不是[错误地]为其添加额外的白色。

使用列表,可以这样实现:

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

你可以通过set()对象或Counter()对象找到更多简洁的方法来获得更多的Python经验......但是看看你能否看到上述工作原理。

这是我的测试用例:我将密码设置为“12345”,然后进行了这些测试:

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

这是你要找的结果吗?

答案 2 :(得分:-1)

关于masterMind()部分:hint()调用在错误的位置执行。它还使用password而非Password作为参数

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

关于提示部分:

answer未在任何地方定义,我认为您的意思是password

我不确定你是否可以使用这样的整数,但如果将它们转换为字符串就可以了。

最后,算法结构不能很好地工作。如果猜测的数量位于正确的位置,则猜测函数将同时回显xo

您应该使用if ... elif结构,然后您可以添加else子句以通知用户该号码根本不在密码中!

尝试使用这些指示重写你的hint()函数,但是如果你需要更多的帮助,这是一个有效的解决方案。

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.

此外,最好检查用户的猜测是否正好有五位数。如果它少于五位,则会出错。

相关问题