如何正确检查列表项和日志(如果已检查)?

2024-09-27 21:29:44 发布

您现在位置:Python中文网/ 问答频道 /正文

试图使破解代码控制台游戏类似的游戏称为“策划”的学校项目。例如:http://www.webgamesonline.com/mastermind/ 白支票应该是:正确的数字,但错误的位置。 重新检查应该是:正确的号码和位置。你知道吗

但是,如果我运行代码,它会出错,并告诉我有1个白点和1个红点,这取决于猜测的数字的位置,而不管最后是否给出了答案。你知道吗

玩家在1-4的范围内选择4个数字。 如果玩家猜到了正确的数字,但它在错误的位置,它应该显示一个白点,以表明这一点。 如果玩家猜到了正确的数字和位置,它应该显示一个红点来表示。你知道吗

问题是,如果我像这样运行代码,它将首先检查第一个数字,如果它是正确的数字,但在错误的位置,它将添加到白名单。你知道吗

如果在代码后面检查另一个数字时,为完全相同的数字指定了正确的数字和位置,则不会发生这种情况。你知道吗

我试着把猜出来的答案附加到一个空的清单上,但是我需要程序一次检查所有的答案,这样如果在最后有一个相同的数字的红格子,它就不会给我一个白格子。你知道吗


   import random


code = []
attempts = 3

while len(code) != 4:
    for x in range(4):
        n = random.randint(1, 4)
        if n not in code:
            code.append(n)
print(code)

pos1 = str(code[0])
pos2 = str(code[1])
pos3 = str(code[2])
pos4 = str(code[3])

answer = str(pos1) + str(pos2) + str(pos3) + str(pos4)

guess = None

while guess != answer:

    positionguess1 = str(input("position 1: "))
    positionguess2 = str(input("position 2: "))
    positionguess3 = str(input("position 3: "))
    positionguess4 = str(input("position 4: "))
    checklist = []
    whitecheck = 0
    redcheck = 0

    """ Row 1 code check """
    if positionguess1 == pos1:
        redcheck += 1
        checklist.append(positionguess1)
    elif positionguess1 != pos1 and positionguess1 in answer and positionguess1 not in checklist:
        checklist.append(positionguess1)
        whitecheck += 1

    """ Row 2 code check """
    if positionguess2 == pos2:
        redcheck += 1
        checklist.append(positionguess2)
    elif positionguess2 != pos2 and positionguess2 in answer and positionguess2 not in checklist:
        checklist.append(positionguess2)
        whitecheck += 1

    """ Row 3 code check """
    if positionguess3 == pos3:
        redcheck += 1
        checklist.append(positionguess3)
    elif positionguess3 != pos3 and positionguess3 in answer and positionguess3 not in checklist:
        checklist.append(positionguess3)
        whitecheck += 1

    """ Row 4 code check """
    if positionguess4 == pos4:
        checklist.append(positionguess4)
        redcheck += 1
    elif positionguess4 != pos4 and positionguess4 in answer and positionguess4 not in checklist:
        checklist.append(positionguess4)
        whitecheck += 1

    crackattempt = str(positionguess1) + str(positionguess2) + str(positionguess3) + str(positionguess4)

    print ("You've entered:", crackattempt)

    if crackattempt == answer:
        print ("Amount in wrong position with right value:", whitecheck)
        print("Amount in the right position and the right value:", redcheck)
        print ("cracked the code, you win")
    elif attempts == 0:
        print ("you lose.")
        break
    elif crackattempt != answer:
        print ("Wrong! Try again.")
        print("Amount in wrong position with right value:", whitecheck)
        print("Amount in the right position and the right value:", redcheck)

        attempts -= 1

如果随机生成的代码是例如“1234”,猜测的代码是“4234”,它应该给我3个redcheck而不是3个redcheck+1个whitecheck

我想我把红支票和白支票分开就解决了。你知道吗


    """ correct number and correct position check """
    if positionguess1 == pos1:
        redcheck += 1
        checklist.append(positionguess1)


    if positionguess2 == pos2:
        redcheck += 1
        checklist.append(positionguess2)


    if positionguess3 == pos3:
        redcheck += 1
        checklist.append(positionguess3)


    if positionguess4 == pos4:
        checklist.append(positionguess4)
        redcheck += 1

    """ correct number but wrong position checks """

    if positionguess1 != pos1 and positionguess1 in answer and positionguess1 not in checklist:
        checklist.append(positionguess1)
        whitecheck += 1

    if positionguess2 != pos2 and positionguess2 in answer and positionguess2 not in checklist:
        checklist.append(positionguess2)
        whitecheck += 1

    if positionguess3 != pos3 and positionguess3 in answer and positionguess3 not in checklist:
        checklist.append(positionguess3)
        whitecheck += 1

    if positionguess4 != pos4 and positionguess4 in answer and positionguess4 not in checklist:
        checklist.append(positionguess4)
        whitecheck += 1

Tags: andanswerinifcode数字checklistappend
2条回答

编辑-每个编辑不属于同一个游戏,但不会删除,因为OP的代码中有有用的想法

下面的代码是实现什么,我相信是游戏。你知道吗

import random

code = random.randint(1000, 9999)

def checks(code, guess):
    """Return tuple with redchecks and whitechecks"""
    code = str(code)
    guess = str(guess)
    redcheck = sum([code[i]==guess[i] for i in range(0, 4)])
    whitecheck = sum([
      code[i]!=guess[i] and (code[i] in guess)
      for i in range(0, 4)
    ])
    return (redcheck, whitecheck)

while True:
    try:
        guess = int(input('Guess:'))
    except:
        print('Exit game')
        break
    if code == guess:
        print('You win!!')
        break
    print(checks(code, guess))

你的错误在逻辑上。让我们看看这个片段

""" Row 1 code check """
elif positionguess1 != pos1 and positionguess1 in answer and positionguess1 not in checklist:
    checklist.append(positionguess1)
    whitecheck += 1

这里的代码检查用户在第一行输入的数字是否在另一行中。在重复的数字的情况下,例如在您的示例中(用户输入4234,正确答案1234),它发现4不在第一行,但存在,因此它添加了白色复选框。checklist用于排除已经测试过的数字,但是作为测试的第一行,checklist是空的,因此将始终添加一个whitecheck。你知道吗

要解决这个问题,您应该首先检查确切的数字,而不是按行检查,并将它们全部添加到检查表中(如果有的话)。然后,检查错误位置的正确数字。你知道吗

相关问题 更多 >

    热门问题