15,958,319 members
1.00/5 (1 vote)
See more:
There is a game. You guess 4 digits of a random number. It outputs the amount of correct digits in the correct place but also correct digits in the wrong place. Ex if the code was 2188 and guess output would be 1888 1 and 1. This piece of code causes trouble logically. Assume tag_guess is an array of 4 falses.

The problem is that it outputs incorrectly. When the code was 4405 and guess 4444 it output 2 correct BUT 2 correct in wrong place. Logically speaking it should output 2 correct 0 correct in wrong place. It should break out of the if statement and ignore the 44 in 4405 as it has already been compared and marked as true. I need to make an array named tag_code and sum it all up using a for and while loop. How do I do so?

Python
```wronPlace= 0
if tag_guess[0]== False:
if guess[0]== code[1]:
tag_guess[0]== True
wronPlace= (wronPlace+1)
elif guess[0]== code[2]:
tag_guess[0]== True
wronPlace= (wronPlace+1)
elif guess[0]== code[3]:
tatg_guess[0]== True
wronPlace= (wronPlace+1)
if tag_guess[1]== False:
if guess[1]== code[0]:
tag_guess[1]== True
wronPlace= (wronPlace+1)
elif guess[1]== code[2]:
tag_guess[1]== True
wronPlace= (wronPlace+1)
elif guess[1]== code[3]:
tag_guess[1]== True
wronPlace= (wronPlace+1)
if tag_guess[2]== False:
if guess[2]== code[0]:
tag_guess[2]== True
wronPlace= (wronPlace+1)
elif guess[2]== code[1]:
tag_guess[2]== True
wronPlace= (wronPlace+1)
elif guess[2]== code[3]:
tag_guess[2]== True
wronPlace= (wronPlace+1)
if tag_guess[3]== False:
if guess[3]== code[0]:
tag_guess[3]== True
wronPlace= (wronPlace+1)
elif guess[3]== code[1]:
tag_guess[3]== True
wronPlace= (wronPlace+1)
elif guess[3]== code[2]:
tag_guess[3]== True
wronPlace= (wronPlace+1)
```

What I have tried:

I've tried doing a for loop but I suck at doing anything more complex until I can lay down a basic layer of understanding toward the simple code first. The code should skip over numbers in the code and guess which have been marked as true/correct as the other. Help!
Posted
Updated 3-Apr-18 6:03am
v3

## Solution 1

Looks like you have donne a step in right direction, but it is still more complicated.
When a guess match a code in correct position, you have to make sure the same guess will not be checked for wrong position and that same code will not be checked for wrong position either. And when a guess match a code for wrong position, you have to make sure you will not check that code again.

Advice: make guess an array just like code and arr, it open the door for loops and code size reduction once it works.

[Update]
Quote:

you have created `arr` to prevent matching 2 times the same guess, you need another array to prevent matching 2 times the same code.
- naming an array `arr` is a bad idea, since it is used to tag matched guess, you can name it `tag_guess`.
- create another array named `tag_code` and you use it to prevent marching 2 times the same code.
- Rather than using variables for each guess, use an array. It will make your code suited for use of loops (and code size reduction).

[Update]
Python
```if tag_guess[0]== False:
if guess[0]== code[1] and tag_code[1]== False:
tag_guess[0]== True
tag_code[1]== True
wronPlace= (wronPlace+1)```

v3
Member 13672432 17-Feb-18 13:13pm
Patrice T 17-Feb-18 16:44pm
see solution update.
Member 13672432 20-Feb-18 16:06pm
how should the array tag_code be ordered?
Patrice T 20-Feb-18 16:32pm
Same as code

## Solution 2

```Create a list that contains the answer.
Loop1:
Compare each digit of the guess with each digit of the list.
For each point in the answer where the digits match do:
Remove the digit from the list, and the guess, or replace them with some other character
Add 1 to the "right digit in the right place" count
Repeat Loop1 for all digits in the guess

Loop2:
For each remaining digit in the guess
If the digit can be found in the answer then do:
Add 1 to the "right digit in the wrong place" count
remove the digit from the answer.
Repeat Loop2 until all comparisons have been made, or no digits remain in either list```

Member 13672432 17-Feb-18 13:14pm
Thanks I'll try doing this but am still a little confused as how to program it. Should it be a while loop or a for?
Richard MacCutchan 18-Feb-18 3:02am
I used a `for` loop in each case, using a `range()` based on the length of the list. You need a `while` loop round the whole thing to allow the user to enter more guesses.
Member 13672432 20-Feb-18 16:16pm
thank you for the help!

## Solution 3

You don't really need 2 for loops. Try this:

```from collections import Counter

def main():
answers = [3, 3, 4, 4]
guesses = [3, 5, 6, 4]

guesses_counter = Counter(guesses)

number_of_correct_digits_in_correct_place = len(
list(filter(lambda x: x == 0, [a - g for a, g in zip(answers, guesses)])))

number_of_correct_digits_in_wrong_place = number_of_all_correct_digits - number_of_correct_digits_in_correct_place

return number_of_correct_digits_in_correct_place, number_of_correct_digits_in_wrong_place

if __name__ == "__main__":
r1, r2 = main()
print(r1)
print(r2)

""" r1 and r2 are the correct digits in wrong place and correct digits in correct place```

You of course need to make "guesses" and actual input for the array but here's the solution.

v2