jeudi 9 mai 2019

How to properly check for list item and log if it has been checked?

Trying to make a crack the code console game similar to the game called " Mastermind " for a school project. whitecheck should be: correct number but wrong position. redcheck should be: correct number and position.

however, if i run the code it will bug out and tell me there's 1 whitecheck and 1 redcheck depending on the position of the guessed number regardless of wether the answer has been given at the end.

I've tried appending the guessed answers to an empty checklist but i need the program to check them all at once so it doesnt give me a whitecheck if there's a redcheck for the same number at the end.


    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


If the randomly generated code is e.g. ' 1234 ' and the guessed code is ' 4234, it should give me 3 redcheck instead of 3 redcheck + 1 whitecheck




Aucun commentaire:

Enregistrer un commentaire