Mastermind Game in Python
Question: I have already looked at this thread [Python Mastermind Game
Troubles](http://stackoverflow.com/questions/15648407/python-mastermind-game-
troubles) on your website but the question I am asking is slightly different
as I have been given a different task.

This is the question:

**Generate a random four digit number. The player has to keep inputting four
digit numbers until they guess the randomly generated number. After each
unsuccessful try it should say how many numbers they got correct, but not
which position they got right. At the end of the game should congratulate the
user and say how many tries it took.**

I have made this so far:

    
    
    from random import randint
    
    n1 = randint (1,9)
    n2 = randint (1,9)
    n3 = randint (1,9)
    n4 = randint (1,9)
    numberswrong = 0
    print (n1,n2,n3,n4)
    guess1 = input("guess the first number")
    guess2 = input("guess the second number")
    guess3 = input("guess the third number")
    guess4 = input("guess the fourth number")
    guess1 = int (guess1)
    guess2 = int (guess2)
    guess3 = int (guess3)
    guess4 = int (guess4)
    if guess1 != n1:
        numberswrong +=1
    else:
        numberswrong +=0   
    if guess2 != n2:
        numberswrong +=1
    else:
        numberswrong +=0 
    if guess3 != n3:
        numberswrong +=1
    else:
        numberswrong +=0 
    if guess4 != n4:
        numberswrong +=1
    else:
        numberswrong +=0 
    print ("you got",numberswrong, "numbers wrong")
    if numberswrong == 0:
        print ("Well done")
    while numberswrong != 0:
        guess1 = input("guess the first number")
        guess2 = input("guess the second number")
        guess3 = input("guess the third number")
        guess4 = input("guess the fourth number")
        guess1 = int (guess1)
        guess2 = int (guess2)
        guess3 = int (guess3)
        guess4 = int (guess4)
        if guess1 != n1:
            numberswrong +=1
        else:
            numberswrong +=0   
        if guess2 != n2:
            numberswrong +=1
        else:
            numberswrong +=0 
        if guess3 != n3:
            numberswrong +=1
        else:
            numberswrong +=0 
        if guess4 != n4:
            numberswrong +=1
        else:
            numberswrong +=0 
        print ("you got",numberswrong, "numbers wrong")
    print ("Well done")
    

The print (n1,n2,n3,n4) is just to test the code and won't be there in the
real code. I am having two problems. Firstly, if you guess the number first
time, two "Well done"'s are shown and second how do I clear the numberswrong
variable without ruining the while loop.If I don't do anything the
numberswrong variable goes up everytime you get an incorrect guess well past
four so even when you guess right, you don't get the well done message as
numberswrong equals like 25 or something similar. Also, if you just say
numberswrong=0 at the end of the while loop the while loop thinks you got it
correct and says well done. I don't know what the solution is. A loop counter
maybe?

Help would be appreciated

Edit: I see why the "Well Done" is printed twice. As for the second query.
Still can't see it.

Edit No.2: @moose. I will answer this as best as I can. When I press run the
program says the number asks me what numbers 1-4 are and then tells me the
number of numbers I have got wrong. Only problem is the number of numbers goes
up and isn't cleared. Lets say I got all the numbers wrong 3 times. On the
third time it says you got 12 numbers wrong. Obviously I can't get 12 numbers
wrong because there is only 4 numbers to guess. I see what is happening the
while loop is not clearing after each pass. I think I know the solution. Say
somewhere numberswrong=0 to clear the loop. But I don't know where to put this
statement.


Answer: Here is the updated code! I’ve used a variable c to keep track of the number
of tries. I also break out of the infinite loop when the user has correctly
guessed the number.

    
    
    from random import randint
    
    n1 = randint(1,9)
    n2 = randint(1,9)
    n3 = randint(1,9)
    n4 = randint(1,9)
    c = 1
    
    while True:
        print (n1,n2,n3,n4)
        guess1 = input("guess the first number")
        guess2 = input("guess the second number")
        guess3 = input("guess the third number")
        guess4 = input("guess the fourth number")
        guess1 = int(guess1)
        guess2 = int(guess2)
        guess3 = int(guess3)
        guess4 = int(guess4)
        numberswrong = 0
    
        if guess1 != n1:
            numberswrong += 1
        if guess2 != n2:
            numberswrong += 1
    
        if guess3 != n3:
            numberswrong += 1
    
        if guess4 != n4:
            numberswrong += 1
    
        if numberswrong == 0:
            print('Well Done!')
            print('It took you ' + str(c) + ' ries to guess the number!')
            break
        else:
            print('You got ' + str(4-numberswrong) + ' numbers right.')
        c += 1
    

