# programming algorithm - bulls and cows game [closed]

I am working on a Bulls and cows game.

And I have the following code that calculated the number of bulls and cows:

let User and comp be the lists that have the 3 digits that the user entered and the number randomly generated by the comp

``````cows = 0

bulls = 0

for x in User:

if x in comp:

if comp.index(x) == User.index(x):

bulls += 1

else:

cows += 1
``````

This works completely fine, unless the number generated by the computer has repeated digit(like 277) and I enter 447. then what happens is that I get 1 cow and zero bulls. Whereas I should get 1 bull and 0 cows.

Note that there is no issue with the game when there are no repeated digits.

I am unable to get what the problem is and why it is occurring. I would greatly appreciate any kind of help in understanding the root of this problem.

more importantly I want to get this done with. So I want a clarification on this approach or I want a new way to think about this.

I am using Python Tkinter to program this game.

-

## closed as off topic by Karl Bielefeldt, GlenH7, kevin cline, MichaelT, DynamicJun 24 '13 at 14:58

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

When `x` is 7, and the computer is 277 and User is 447 then `User.index(x)` is 2 but `comp.index(x)` is 1, so it doesn't come up a bull. You have to re-arrange the whole thing so you cycle through all indexes (0, 1, and 2) and for each time through decide if that index is a bull or cow.