#๐Ÿ”’ ValueError: too many values to unpack

37 messages ยท Page 1 of 1 (latest)

warm nest
#
names_and_dob = [] #empty list to hold user input
num_employee = int(input("Number of employees : ")) #determines the number of employees

print("First name, last name, D.O.B.(Hit ENTER after each input):")

for employee in range(0, num_employee): #creates arrays for each given employee
    user_given = [input(), input(), input()]
    names_and_dob.append(user_given) #adds each individual variable to an array through .append

def get_date(names_and_dob):
    dob = []
    for date in names_and_dob:
        dob.append(date[2])
    return dob

print(names_and_dob)

import datetime
birthdate = "/".join(get_date(names_and_dob))
month,day,year = map(int, birthdate.split("/"))
today = datetime.date.today()
age = today.year - year - ((today.month, today.day) < (month, day))
print(age)``` I'm currently working on this code for class and the current problem I'm facing is when I try to calculate for age in the most recent text, my code can only handle the input for one date (ValueError: too many values to unpack (expected 3)). I know the issue lies within the variable birthdate but I'm unsure of the proper way to set it up? Because before I tried adding "/".join it wouldnt work because it was a list of strings.
fast flumeBOT
#

@warm nest

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.

stuck galleon
#

What are you intending get_date to do?

warm nest
#

my code asks for a first name, last, and date of birth, get_date separates the date given from the other input so I can use it to calculate age from that date

#

I know it doesnt make sense lol, but I messaged my TA about it days ago a few times and he hasnt got back to me๐Ÿ˜ญ

stuck galleon
#

Or, in other words, you're trying to use a list of employee birthdates to get a single birthdate. Did you mean to calculate a birthdate for each employee?

warm nest
#

Yes that is what I meant to do, I understand that " ".join puts them all into one list but before when I tried just birthdate = (get_date(names_and_dob)), there is no attribute to split in the list

noble hemlock
#

!e

items = ["A", "B", "C"]
print(type(", ".join(items)))
fast flumeBOT
#

@noble hemlock :white_check_mark: Your 3.12 eval job has completed with return code 0.

<class 'str'>
warm nest
#

apologies, i havent been coding for long so I slip up on termanolgy but i appreciate the correction

noble hemlock
#

user_given = [input(), input(), input()] is creating a list.
names_and_dob.append(user_given) is appending that list to another list. #adds each individual variable to an array through .append It's not adding each variable, like you think it is.

#

!e

dob_list = []
user_given = ["john", "doe", "01/01/91"]
dob_list.append(user_given)
print(dob_list)
fast flumeBOT
#

@noble hemlock :white_check_mark: Your 3.12 eval job has completed with return code 0.

[['john', 'doe', '01/01/91']]
noble hemlock
#

So now you have a nested list.

#

user_given = [input(), input(), input()] This part would be a nightmare if you had to put in... let's say 20 users. You would have to do 3 inputs per user, multiply that by 20 users...

#

You can do something a little simpler

user_data = input("Enter first name, last name, dob seperated by a space: ").split(" ")
# example input 'John Doe 01/01/21'
#

but where you're having the issue at...

#

This is what is in birthdate

john doe 1/1/1
jane doe 2/2/2
[['john', 'doe', '1/1/1'], ['jane', 'doe', '2/2/2']]
1/1/1/2/2/2 # contents of birthdate
#

birthdate = "/".join(get_date(names_and_dob)) combines all the dob's in the list, into one string.

#

So, you're trying to unack 3 values... from 6 that are there.

#

Which increases the more users you have.

#
john doe 1/1/1
jane doe 2/2/2
bob smith 3/3/3
[['john', 'doe', '1/1/1'], ['jane', 'doe', '2/2/2'], ['bob', 'smith', '3/3/3']]
1/1/1/2/2/2/3/3/3 # contents of birthdate
warm nest
#

I understand that is where my issue is and what it is, I'm just unsure of how to fix it

noble hemlock
#

So, names_and_dob is a list that contains a list of people and their information.

#

Each item in that list has list object that has a first name, last name, and dob.

#

one second...

#

!e

people = [["john", "doe", "1/1/1"], ["jane", "doe", "2/2/2"], ["bob", "smith", "3/3/3"]]

for fname, lname, dob in people:
    print(fname, lname, dob)
fast flumeBOT
#

@noble hemlock :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | john doe 1/1/1
002 | jane doe 2/2/2
003 | bob smith 3/3/3
warm nest
#

i do not understand how this relates to my original issue?

noble hemlock
#

Sorry, juggling this and cooking. Okay, so now you have the dob.

#

!e

people = [["john", "doe", "01/01/1986"], ["jane", "doe", "02/20/2000"], ["bob", "smith", "03/30/2013"]]

for fname, lname, dob in people:
    month, day, year = dob.split("/")
    print(month, day, year)
fast flumeBOT
#

@noble hemlock :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 01 01 1986
002 | 02 20 2000
003 | 03 30 2013
fast flumeBOT
#
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.