-3

Something is going wrong at the line:

copy_guess[:], copy_code[:] = zip(*((a, b) for a, b in zip(guess,code) if a != b))

error massage is:

ValueError: not enough values to unpack (expected 2, got 0)
import random
max_range = 5
code_lines = 4
def generate_code():
    code_arr = []
    for i in range(code_lines):
        code_arr.append(random.randint(0,max_range))
    return code_arr
def right_position(guess,code):
    right_positions = 0
    answer_position = [len(set(i)) == 1 for i in zip(guess,code)]
    for x in answer_position:
        if x == True:
            right_positions += 1
    return right_positions
def all_positions_right(guess,code):
    answer_position = [len(set(i)) == 1 for i in zip(guess,code)]
    return answer_position

this is where the problem is i think. it worked quite good without this but not evry time.

def wrong_position(guess,code):
    char_number = 0
    copy_guess = guess
    copy_code = code
    copy_guess[:], copy_code[:] = zip(*((a, b) for a, b in zip(copy_guess,copy_code) if a != b))
    for a in copy_guess:
        for b in copy_code:
            if a == b:
               char_number += 1   
    return char_number

the code is split in two files.

from mastermind_funcs import generate_code, right_position, wrong_position , all_positions_right

code_lines = 4
def guesslist_func():
    i = 1
    guess_list = []
    while i <= code_lines:
        x = int(input("guess number "))
        guess_list.append(x)
        i += 1
    return guess_list
def main():
    right_code = generate_code()
    print(right_code)
    true_list = [True,True,True,True]
    value = True
    guess_count = 0
    while value:
        guess_list = guesslist_func()
        if all_positions_right(guess_list,right_code) == true_list:
            value = False
            print("grattis, coden var " , right_code , ".")
        else:
            if right_position(guess_list,right_code) > 0:
                print(right_position(guess_list,right_code))
                print(wrong_position(guess_list,right_code))
            else:
                print(wrong_position(guess_list,right_code))
        guess_count += 1
    print("du klarade det på" , guess_count , "gissningar.")
if __name__ == "__main__":
    main()

evrything works fine except wrong_position

buran
  • 13,682
  • 10
  • 36
  • 61
Milloz
  • 55
  • 4
  • 3
    We're going to need some sample data (`guess_list` and `right_code` at least) in order to help. Please provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Michael Ruth Oct 05 '22 at 20:00
  • 1
    Your `wrong_position` ode works fine for me, if I pass it two arrays of 4. What you passing it? Have you printed `guess` and `code` to know what you're getting? If either of those lists are empty, then I get that error. – Tim Roberts Oct 05 '22 at 20:03
  • You don't have to initialize `copy_guess`, `copy_code`, or `guess_list`. Just do `copy_guess, copy_code = zip(...)`. – Tim Roberts Oct 05 '22 at 20:06
  • What is the code of `all_positions_right`? The error suggest that guess_list and right_code hold the same numbers – buran Oct 05 '22 at 20:07
  • code is a randomly generated list of 0-5 and guess is your guess, all_position_right is just a def to see if both lists are the same and end the loop – Milloz Oct 05 '22 at 20:11
  • I understand what you think it (all_position_right) does, but I guess it doesn't work as you expect, because the error would happen when guess numbes at all positions are correct – buran Oct 05 '22 at 20:13
  • "Something is going wrong at the line:" Okay, so **did you try to check** what the result of the `zip` is (for example, by creating and displaying a `list` of the values)? Did you try to check the value of `guess` and of `code` when the problem occurs? (Incidentally, do you know that `copy_guess = guess` **does not** copy anything?) Please read [ask] and [mre] and keep in mind that this is not a debugging service. – Karl Knechtel Oct 05 '22 at 21:07

1 Answers1

1

OK, the problem is that inside wrong_position function you mutate guess and code and thus also guess_list and right_code. You can print the 2 lists after you call wrong_position function to see for your self and debug.

Here is minimal reproducible example to demonstrate what is going on

def eggs(foo):
    bar = foo
    bar[::] = [4, 5, 6]
    print(f'foo: {foo}')
    print(f'bar: {bar}')

spam = [1, 2, 3]
eggs(spam)
print(f'spam: {spam}')

output

foo: [4, 5, 6]
bar: [4, 5, 6]
spam: [4, 5, 6]

So, to fix the error, change

copy_guess = guess
copy_code = code

to

copy_guess = guess[::]
copy_code = code[::]

Further reading How do I clone a list so that it doesn't change unexpectedly after assignment?

That said, there are a lot things in this code that can and should be done differently to be more pythonic.

buran
  • 13,682
  • 10
  • 36
  • 61
  • If that's the problem, then this is a duplicate of the linked question and should be closed (as a duplicate) and deleted (as not a useful signpost, and for showing a complete lack of debugging effort). – Karl Knechtel Oct 05 '22 at 21:11
  • @KarlKnechtel, I have already voted to close it for lack of debug details before they added the full code. If I retract, I cannot vote again. Decided just to help instead and it is too long for comment. I don't mind if it is closed and/or to delete my answer – buran Oct 05 '22 at 21:31