Why do I get a "too many values to unpack (expected 2)" error when my function reruns itself

my function is for editing the “patient” dictionary. It runs smoothly on the first run but i get a “too many values to unpack (expected 2)” on “for k, v in patient.items():” once the program reruns itself (the else part).

def approval():
    print('do you approve the information below?')
    print(' ')
    number = 0
    change = []
    for k, v in patient.items():
        number += 1
        print('%s. %s: %s' % (number, k, v))
        change.append(k)
    print(' ')
    a = int(input('In case of approval send 100, else send the number that must be edited '))
    if a == 100:
        print('!Your information has been approved!')
    else:
        c = str(input('Please enter the correct %s : ' % (change[a-1])))
        patient[change[a-1]] = c
        approval()

Can you paste in the whole error message and the traceback? Normally it
will show the exact line involved and important other information.

On the face of it, this line:

 for k, v in patient.items():

should work just fine, because dict.items yields 2-tuples.

One possibility is that the error is for another line or that patient
isn’t what you thought (not a dict). Try printing out the value of
patient just before the for-loop to ensure it is what you expect.

Cheers,
Cameron Simpson cs@cskk.id.au

weirdly, the whole problem has just disappeared :sweat_smile: :sweat_smile:
I haven’t changed the code, but, poof, there is no problem now. I can edit it as much as I want
Just another question, how can I break strings so that my code can fit pep8?

weirdly, the whole problem has just disappeared :sweat_smile:
:sweat_smile:
I haven’t changed the code, but, poof, there is no problem now. I can edit it as much as I want

I suspect the problem occurred with a slightly earier version of the
code than that which you posted (which looked fine to me).

BTW, recursion is not a good way to do something “forever”. Use a while
loop.

Just another question, how can I break strings so that my code can fit
pep8?

Adjacent strings are concatenated. So:

 "a very long string"

can be written:

 "a very long" " string"

A common idiom to extend an expression across multiple lines is to put
it in brackets, eg:

 s = (
     "a very long"
     " string"
 )

Cheers,
Cameron Simpson cs@cskk.id.au

This is why it’s important to make sure you can produce a proper minimal reproducible example of issues. The gold standard is to make sure, before posting:

  • Does this code look right in the preview?
  • Can I copy this code out of the preview into a new file and run it without adding or changing anything?
  • When I do that, can I directly see the exact problem I want to ask about, without interacting with the program?