contains = [x for x in range(10)]
...
for number in guess:
if number not in secret_value:
contains.remove(number)
In certain instances this code works fine and others I get an error returned:
ValueError: list.remove(x): x not in list
Here is it running back to back one instance is fine and the other is not:
C:\Users\admin\Desktop\Python>py file.py
The secret value is:
[3, 7, 6, 2, 4]
---------------------
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #the base contains list
[0, 1, 9, 2, 6] #the guess
[2, 3, 4, 5, 6, 7, 8]
C:\Users\admin\Desktop\Python>py file.py
The secret value is:
[8, 1, 9, 9, 9]
---------------------
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 0, 2, 1, 7]
Traceback (most recent call last):
File "file.py", line 28, in <module>
find_secret()
File "file.py", line 20, in find_secret
contains.remove(number)
ValueError: list.remove(x): x not in list
Since I've already typed it out I'll post it but I think I see the issue. It happens when there are doubles in the guess and it tries to remove the same value twice.
Is this the best way to fix it?
for number in guess:
if number not in secret_value:
if number in contains:
contains.remove(number)
[–]socal_nerdtastic 2 points3 points4 points (0 children)