I have got an existing menu that gives you options L
or D
. L
should load the contents of a file and D
should display it.<
If you want to work around that without adding extra lines and checks, one solution is to change your split to the following:
name, adult, child, garbage = (line+',,,').split(',', maxsplit=3)
This will silently ignore the missing values, and the variables will be empty. Meaning, the values for name
, adult
and child
will be filled if they are there, and will be empty it the original file doesn't have them. For all intents and purposes, ignore the variable garbage
.
This means that there is a line in packages.txt
that, when you strip whitespace and split on commas, doesn't give exactly three pieces. In fact, it seems that it gives only 1 piece ("need more than 1 value to unpack"), which suggests that there's a line with no commas at all.
Perhaps there are blank or comment lines in packages.txt
?
You may need your code to be smarter about parsing the contents of the file.
line.split(',')
returns a tuple. You then un-pack that tuple by writing:
name,adult,child= line.split(',')
If the tuple does not have exactly three elements then the un-packing fails. In your case the error message states that you have only one element. So, line.split(',')
is clearly returning a tuple with only one element. And that means that line
has no commas.
Probably this means that your input data is not what you expect it to be. You require that line
is a string containing three comma separated values but there is a line in your input data that does not meet that requirement.
This error is occurring at
name,adult,child= line.split(',')
When you assign three variables on the left it is assuming you have a 3-tuple on the right. In this example, it appears line
has no comma hence line.split(',')
results in a list with only one string, thus the error "more than 1 value to unpack".