Bulls and Cows Game - Python programming for beginners
Question: I am a beginer programming Python and for a evaluation assignment I was asked
to program a Bulls and Cows game using very simple code. I have some code
already written, but can't seem to make the function work. The function should
be able to compare the four digit number the user wrote (num_guess) to the
random number generated by the program (stored in a list called num). I can
input the value, but have no further feedback from the program. Could you
please check my code and help me? Thanks!

    
    
    import random
    
    guess = raw_input("What is your guess? ") 
    if (len(guess) != 4): 
        guess = raw_input("Your guess must be 4 characters long! What is your guess?")
    
    num_guess = []  
    for c in guess:  
        num_guess.append(int(c)) 
    
    def compare(num_guess):
        num = []  
        for i in range(4):  
            n = random.randint(0, 9)  
            while n in num: 
                n = random.randint(0, 9)
            num.append(n)
    
     if num_guess == num: 
         print "Congratulations! Your guess is correct"
    
     output = []
    
     if num_guess[0] == num [0]:
        output.append["B"]
     else:
         output.append["C"]
    
     if num_guess[1] == num [1]:
         output.append["B"]
     else:
         output.append["C"] 
    
     if num_guess[2] == num [2]:
         output.append["B"]
     else:
         output.append["C"] 
    
     if num_guess[3] == num [3]:
         output.append["B"]
     else:
         output.append["C"]
    
     return output
    
    nguesses = 0
    while nguesses < 11:
    nguesses = nguesses + 1, compare 
    


Answer: Without giving you the corrected code you will want to call the function at
the end of your code. If you want to see result do:

    
    
    print compare(num_guess)
    

This will show you an additional issue with all of these parts

    
    
    output.append[]
    

I'm not sure if it is just how you pasted it but you may want to clean up the
indentation. Also:

    
    
    while n in num: 
            n = random.randint(0, 9)
    

This above section is not needed. Have fun and figure out how to make it work
well.

