亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Mastermind 基于文本,無法識別密碼中的正確數字

Mastermind 基于文本,無法識別密碼中的正確數字

滄海一幻覺 2021-08-05 15:35:30
我的代碼沒有正確計算正確的數字,示例:(注意,我是故意打印密碼的(列表))    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 91  of 5 correct digits.4  of 5 correct locations.9  guesses remaining.>5 位數字中有 4 位是正確的,只計算“5 中的 1”。這是我的代碼:import randomdef 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_listdef 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 函數中;我被困住了。
查看完整描述

1 回答

?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

你的問題是不正確的縮進:


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

您的外循環的索引為i,然后您可以在第二個內循環中劫持并更改該索引。這將i超過循環限制,在第一次迭代時退出外循環。


您需要將第二個“內部”循環和所有后續代碼拉回到其正確位置,向左縮進一個,離開第一個循環。


def reportResult(password,guess):

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

    numCorrect = 0

    posNumCorrect = 0

    for i in range(len(password)):

        for j in range(len(guess)):

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

                numCorrect = numCorrect+1

    for i in range(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

輸出:


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  guesses remaining.

> 8 3 4 5 6

4  of 5 correct digits.

4  of 5 correct locations.

9  guesses remaining.

> 3 8 4 6 5

4  of 5 correct digits.

1  of 5 correct locations.

8  guesses remaining.


查看完整回答
反對 回復 2021-08-05
  • 1 回答
  • 0 關注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號