'''
https://www.practicepython.org/exercise/2014/07/05/18-cows-and-bulls.html

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
  ...
'''

# Cows and Bulls Game

import string
import random

_gnum=[]
_usernum=[]
cow=0
bull=0
print("Welcome to the Cows and Bulls Game! \n")

def num_gen(a):
    for i in range(a):
        _gnum.append("".join(random.choice(string.digits)))
    #print("Generated number: " + str(_gnum))
    return _gnum

def logic(u, g):
    global cow
    global bull
    for i in range(len(g)):
        if u[i] in g:
            if u[i]==g[i]:
                cow = cow + 1
            for value in g:
                if u[i] == value:
                    bull = bull + 1
    return cow, bull

while True:
    _usernum = input("Enter a 4 digit number ")
    _gnum.clear()
    if len(_usernum)!= 4:
        print("Wrong number lenght ... It'd be 4 digits long... \n")
        break
    if _usernum == num_gen(4):
        break
    if _usernum == "exit":
        break
    else:
        logic(_usernum, _gnum)
        print(str(cow)+" Cows, " + str(bull) + " Bulls \n")

