#๐Ÿ”’ [Solved] Too many values to unpack, but the number of them is the same as expected

37 messages ยท Page 1 of 1 (latest)

naive pebble
#

This is the error I get

Traceback (most recent call last):
  File "C:\Users\jumic\PycharmProjects\SQLThing\KetoCounter.py", line 47, in <module>
    append_mode()
  File "C:\Users\jumic\PycharmProjects\SQLThing\KetoCounter.py", line 39, in append_mode
    for a, b, c in to_append:
        ^^^^^^^
ValueError: too many values to unpack (expected 3)

and this is the part of the code throwing it

for a, b, c in to_append:
            print("|   ", a, "   |   ", str(b), "   |   ", c, "   |")

I'm trying to loop through this tuple and format the output in a prettier way, it's defined as:

to_append = [(str, float, str)]

I haven't worked with python since school so I've probably misunderstood something here.

still folioBOT
#

@naive pebble

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

vestal zinc
#

!e

to_append = [(str, float, str)]
for a, b, c in to_append:
    print("|   ", a, "   |   ", str(b), "   |   ", c, "   |")
still folioBOT
naive pebble
#

I can post the full method

vestal zinc
#

It's weird that you're printing classes, but that won't cause this error.

naive pebble
#

it's about 15 lines

#
def append_mode():
    to_append = [(str, float, str)]
    while True:
        try:
            name = input("What is the name of your dish? ").lower()
            carb_count = float(input("How many grams of carbs does it contain? "))
            per_what = input(str(carb_count) + "g per what? ")
        except ValueError as e:
            print("You typed letters, but I expected numbers")
            continue
        to_append += (name, carb_count, per_what)

        do_i_continue = str
        while do_i_continue != "n" and do_i_continue != "y":
            do_i_continue = input("Would you like to continue? (y/n) ").lower()

        if do_i_continue == "n": break

    del to_append[0]

    do_i_commit = str
    while do_i_commit != "n" and do_i_commit != "y":
        print("You've queued these dishes for upload to the database:\n")
        for a, b, c in to_append:
            print("|   ", a, "   |   ", str(b), "   |   ", c, "   |")
        do_i_commit = input("Would you like to commit these changes? (y/n) ").lower()
vestal zinc
#

to_append += (name, carb_count, per_what)

#

You want to_append.append((name, carb_count, per_what))

naive pebble
#

ah ok makes sense

#

yep that fixes it

vestal zinc
#

!e

to_append = [(str, float, str)]
to_append += (1, 2, 3)
print(to_append)
still folioBOT
naive pebble
#

I was just missing the append() function

#

so what exactly was I doing with += in that case?

vestal zinc
#

+= is effectively the list method extend, so the issue was that you were using extend instead of append. I recomment looking into the diffrerence.

naive pebble
#

ah ok fair enough

#

any way to mark this as solved in that case?

vestal zinc
#

Why are you adding types to the list though? That's purely out of curiosity.

naive pebble
#

I just wanted to pre-define the list

#

didn't really know how without straight up adding the types

vestal zinc
naive pebble
#

[]

#

oh wait nvm not a command mb

#

lemme change the title then

vestal zinc
#

!e

l = []
l.append(1)
print(l)
still folioBOT
naive pebble
#

[Solved] Too many values to unpack, but the number of them is the same as expected

vestal zinc
naive pebble
#

oh ok

#

in that case I'll define it as []

#

thanks for the help

#

!close

still folioBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.