Post a link to the exercise and we can take a look.
Make sure
all_ages
begins with the ages for Ainsley, Ben, and Chani and ends with Depak’s age (stored inage
)!
That would imply,
all_ages = [32, 41, 29] + age
yes, exactly. and if you did the exercise and submitted it this way, it falsely returns an error. And this is why there are no less than 8 people on this thread complaining of the same thing. They are all trying to flag up that if you follow the instructions, the page returns an error. Whereas if you do the concatenation in the reverse order (all_ages = age + [32, 41, 29]) the page allows you to proceed.
After resetting the exercise and going through the steps I was not able to reproduce the above described error.
I think I understand the confusion now, and I was confused as well.
In the exercise 4. it says: " Create a new list called all_ages
that adds age
with the following list…" It implies that age should come before the list, like this: age + [32, 41, 29], which would produce an error and make everyone confused. I did the reverse, the same as mtf, and now it works!
Hello, just another question but it is related to this exercise so please allow me to ask here.
I tried to add one more person and his age on the list of the exercise , but it seemed the age wasn’t printed and I don’t understand why. Here is my code (I put print
on each step to make sure the step return correct value):
#name_04
first_names = ['Ainsley', 'Ben','Chani','Depak']
#age_04
age = []
age += [25]
all_ages = [32,41,29] + age
print(all_ages)
#combine name and age
name_and_age = list(zip(first_names,all_ages))
name_and_age += ('Hook',100)
print(name_and_age)
#IDs
ids = range(1,6)
print(list(ids))
#final list
customer_list = list(zip(ids,name_and_age))
print(customer_list)
And here is what console return:
[32, 41, 29, 25]
[('Ainsley', 32), ('Ben', 41), ('Chani', 29), ('Depak', 25), 'Hook', 100]
[1, 2, 3, 4, 5]
[(1, ('Ainsley', 32)), (2, ('Ben', 41)), (3, ('Chani', 29)), (4, ('Depak', 25)), (5, 'Hook')]
As you can see, the age of Hook is missing while it was successfully added in name_and_age
. Can anyone please help me to explain what wrong was happening in my code?
A failed assumption. Selfless programmers will not dismiss this. Be selfless and dig deeper.
('Depak', 25), 'Hook', 100]
Successfully? Look closely.
Oh, you are right. There is something incorrect right here. Thanks for spotting it out. I am investigating this more and see what caused it.
I think I found that this caused the problem:
name_and_age += ('Hook',100)
In here, the list name_and_age
is added by two separated items ‘Hook
’ and ‘100
’ => It made the name_and_age
contain 6 items while the list ids
only has 5 items. That’s why item ‘100
’ wasn’t added in the customer_list
.
Hence, I changed it to:
name_and_age += [('Hook', 100)]
by doing this, I was trying to add a new list containing a single item('Hook', 100)
into the name_and_age
, I mean Hook
and 100
are considered as a whole now.
And it works:
[32, 41, 29, 25]
[('Ainsley', 32), ('Ben', 41), ('Chani', 29), ('Depak', 25), ('Hook', 100)]
[1, 2, 3, 4, 5]
[(1, ('Ainsley', 32)), (2, ('Ben', 41)), (3, ('Chani', 29)), (4, ('Depak', 25)), (5, ('Hook', 100))]
Please correct me if anything wrong.