#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Exercise 18: Cows And Bulls

Create a program that will play the “cows and bulls” game with the user.
The game works like this:

Randomly generate 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 teh game
and tell the user at the end.

Say the number generated by the computer is 1038. An example interaction
could look like this:

    Welcome to the Cows and Bulls Game!
    Enter a number:
    >>> 1234
    2 cows, 0 bulls
    >>> 1256
    1 cow, 1 bull

Until the user guesses the number.
"""

import random


def get_secret_number():
    return str(random.randint(1000, 9999))


def get_matches(secret, user):
    cows = bulls = 0

    for i in range(4):
        if secret[i] == user[i]:
            cows += 1
        if secret[i] in user:
            bulls += 1

    return cows, bulls


guessed = False
guesses = 0
secret = get_secret_number()

while not guessed:
    user = input("Guess a 4-digit number: ")
    guesses += 1

    if user == secret:
        guessed = True

    print("%i cows, %i bulls" % (get_matches(secret, user)))

print("Congrats! The number was %i. You has %i guesses." % (secret, guesses))

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Exercise 18: Cows And Bulls

Create a program that will play the “cows and bulls” game with the user.
The game works like this:

Randomly generate 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 teh game
and tell the user at the end.

Say the number generated by the computer is 1038. An example interaction
could look like this:

    Welcome to the Cows and Bulls Game!
    Enter a number:
    >>> 1234
    2 cows, 0 bulls
    >>> 1256
    1 cow, 1 bull

Until the user guesses the number.
"""

import random


def get_secret_number():
    return str(random.randint(1000, 9999))


def get_cows_and_bulls(secret, user):
    cows = bulls = 0

    for i in range(4):
        if secret[i] == user[i]:
            cows += 1
        if secret[i] in user:
            bulls += 1

    return cows, bulls


if __name__ == "__main__":

    guessed = False
    attempts = 0
    secret = get_secret_number()

    while not guessed:
        user = input("Guess a 4-digit number: ")
        attempts += 1

        if user == secret:
            guessed = True

        print("%i cows, %i bulls" % (get_cows_and_bulls(secret, user)))

    print(
        "Congrats! The number is %s. You did %s attempts." %
        (secret, attempts))

Update cows_and_bulls.py