Mastermind 基于文本,无法识别密码中的正确数字

我的代码没有正确计算正确的数字,


示例:(注意,我是故意打印密码的(列表))


    I've set my password, enter 5 digits in the range [1-9] (e.g. 9 3 2 4 7):


[1, 5, 6, 7, 8]

10  guesses remaining.

>

1 5 6 7 9

1  of 5 correct digits.

4  of 5 correct locations.

9  guesses remaining.

>

5 位数字中有 4 位是正确的,只计算“5 中的 1”。


这是我的代码:


import random


def generatePassword():

    '''Generates unique 5 digit password'''

    randomSetOfValues = set()

    while len(randomSetOfValues) < 5:

        randomSetOfValues.add(random.randint(1,9))

    return list(randomSetOfValues)


def getUserGuess():

    '''Gets users geuss/input for the password'''

    guess_list=[]

    print('>',)

    a,b,c,d,e=map(int, input().split())

    guess_list.append(a)

    guess_list.append(b)

    guess_list.append(c)

    guess_list.append(d)

    guess_list.append(e)

    return guess_list


def reportResult(password,guess):

    '''Reports users results, see if positions/numbers are correct'''

    numCorrect=0

    posNumCorrect=0

    for i in range(0,len(password)):

        for j in range(0,len(guess)):

            if(guess[j]==password[i]):

                numCorrect=numCorrect+1

        for i in range(0,len(password)):

            if(guess[i]==password[i]):

                posNumCorrect=posNumCorrect+1

        if(posNumCorrect==5):

             return True

        else:

            print(numCorrect," of 5 correct digits.")

            print(posNumCorrect," of 5 correct locations.")

            return False


我认为问题出在我的 reportResult 函数中;我被困住了。


沧海一幻觉
浏览 136回答 1
1回答

潇潇雨雨

你的问题是不正确的缩进:for i in range(0,len(password)):&nbsp; &nbsp; for j in range(0,len(guess)):&nbsp; &nbsp; &nbsp; &nbsp; if(guess[j]==password[i]):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numCorrect=numCorrect+1&nbsp; &nbsp; for i in range(0,len(password)):&nbsp; &nbsp; &nbsp; &nbsp; if(guess[i]==password[i]):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; posNumCorrect=posNumCorrect+1您的外循环的索引为i,然后您可以在第二个内循环中劫持并更改该索引。这将i超过循环限制,在第一次迭代时退出外循环。您需要将第二个“内部”循环和所有后续代码拉回到其正确位置,向左缩进一个,离开第一个循环。def reportResult(password,guess):&nbsp; &nbsp; '''Reports users results, see if positions/numbers are correct'''&nbsp; &nbsp; numCorrect = 0&nbsp; &nbsp; posNumCorrect = 0&nbsp; &nbsp; for i in range(len(password)):&nbsp; &nbsp; &nbsp; &nbsp; for j in range(len(guess)):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(guess[j] == password[i]):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numCorrect = numCorrect+1&nbsp; &nbsp; for i in range(len(password)):&nbsp; &nbsp; &nbsp; &nbsp; if(guess[i] == password[i]):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; posNumCorrect = posNumCorrect+1&nbsp; &nbsp; if(posNumCorrect == 5):&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return True&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print(numCorrect," of 5 correct digits.")&nbsp; &nbsp; &nbsp; &nbsp; print(posNumCorrect," of 5 correct locations.")&nbsp; &nbsp; &nbsp; &nbsp; return False输出:I've set my password, enter 5 digits in the range [1-9] (e.g. 9 3 2 4 7):[8, 2, 4, 5, 6]10&nbsp; guesses remaining.> 8 3 4 5 64&nbsp; of 5 correct digits.4&nbsp; of 5 correct locations.9&nbsp; guesses remaining.> 3 8 4 6 54&nbsp; of 5 correct digits.1&nbsp; of 5 correct locations.8&nbsp; guesses remaining.>&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python