dimanche 10 octobre 2021

python bulls and cows game

"I am writing a code fora game which randomly generates a 4-digit number. Ask the user to guess a 4-digit number. For every digit that the user guessed correctly in the correct place, they have a “cow”. For every digit the user guessed correctly in the wrong place is a “bull.” Every time the user makes a guess, tell them how many “cows” and “bulls” they have. Once the user guesses the correct number, the game is over. Keep track of the number of guesses the user makes throughout the game and tell the user at the end."

same bulls and cows problem

here are the functions i get for bulls and cows but they don't work.

can someone tell me why please

def how_many_bulls(guess,answer):
    bulls = 0 
    i = 0 
    while i < 4:
        if guess[i] == answer [i]:
            bulls = bulls + 1
            i = i + 1 

    return bulls



def how_many_cows(guess,answer):
    answer = secret 
    cows = 0
    a = 0 
    while a<4:
        if answer [i] == guess [i]:
            cows = cows + 1
            a = a+1 

    cows = cows - bulls 
    
    return cows```


Aucun commentaire:

Enregistrer un commentaire