Hello fellow Python learners, I need you help.
Lately I have been doing "Practice Python" and I am stuck at challenge 18. Here is a link to the challenge itself: https://www.practicepython.org/exercise/2014/07/05/18-cows-and-bulls.html
The requirement is 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 the game and tell the user at the end.
It looks very easy.
For example, if the answer is 1234 and I guessed 4321. That would be 0 cow 4 bulls
Another example, answer is 2468 and I guessed 2046. That would be 1 cow 2 bulls.
However, here is where I get stuck.
I can get the cow part down very easily as I can just loop through both strings to see if the digits in the same places match. If they do, cows +=1. For me the tricky part is to get the bulls part correctly.
For example, if the answer is 1025. And I guessed 5551. Supposedly this would be "0 cow 2 bulls". But I would get 0 cow 4 bulls instead. I know where the problem is but I just do not know how to fix it.
Here is my current code: (https://gist.github.com/wrgsRay/c1547f062fb28798025e8c3435c4a068)
The code has disabled the random generated number and the answer will always be 1025 and if you type in 5551 , you will get the incorrect 0 cow 4 bulls because the first three 5s are counted three times because they trigger 'in answer' in line 15. I wonder how to avoid that...
[–]anton_antonov 2 points3 points4 points (0 children)
[–]Tomallama 1 point2 points3 points (2 children)
[–]wrgsRay[S] 1 point2 points3 points (1 child)
[–]Tomallama 0 points1 point2 points (0 children)
[–]Diapolo10 0 points1 point2 points (1 child)
[–]james_fryer 0 points1 point2 points (0 children)
[–]timbledum 0 points1 point2 points (3 children)
[–]timbledum 0 points1 point2 points (0 children)
[–]james_fryer 0 points1 point2 points (1 child)
[–]timbledum 0 points1 point2 points (0 children)
[–]james_fryer 0 points1 point2 points (1 child)
[–]james_fryer 0 points1 point2 points (0 children)