Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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.

please help me with this issue.

share|improve this question

closed as off topic by Karl Bielefeldt, GlenH7, kevin cline, MichaelT, Dynamic Jun 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.

1 Answer 1

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.

share|improve this answer
    
I get the idea but I don't understand how to code it. Please give me the code. –  IcyFlame Jun 24 '13 at 13:25
3  
@IcyFlame Programmers.SE isn't a code writing service. The site is based around design questions, not implementation questions. We especially don't want to be doing people's homework for them - too often we have faced people who are unable to do their job, having had people give them their code all through school (without learning anything themselves). We can help you figure out what is wrong with the design, but please don't ask for "give me the code." –  MichaelT Jun 24 '13 at 14:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.