decades = []
decade=[]
age = []
for age in final_ages:
if age == 'Unknown':
decade.append(age)
else:
decade.append(str(age))
for decade in decades:
decade=decade[:-1] + '0s'
decades.append(decade)
Iām not sure what you need help with in the above code.
It basically checks if age given in final_ages is unknown or not. If it is unknown the string value is appended to the previously empty age list. If the age is any other value other than unknown, it will be converted into a string and appended to age list.
The second for loop adds the string ā0sā at the end of the value stored in decade and updates its own value. This updated value is then appended to decades` list.
Please let me know what exactly is your doubt in this.
I didnāt refer to the course (you didntā put the link), so I donāt know what the good result should be.
I have just reproduced your code removing the syntax error. Look at your initial for loop:
for decade in decades:
decade=decade[:-1] + '0s'
decades.append(decade)
First, when you do this, decades list is empty. Second you want to iterate over decades using decade as a variable inside the for loop, but decade name is already used as a list before ! So I have deducted that you wanted more likely to iterate over decade and not over decades, and I have renamed d the variable inside the for loop. And this is why your lines:
decade=decade[:-1] + '0s'
decades.append(decade)
became:
_str=d[:-1] + '0s'
decades.append(_str)
Maybe this is not the good result indeed, but without refering to the course, hard to know !
But your decades list is empty by the moment you start your second for-loop! Practically, you are iterating through an empty list:
decades = []
decade=[]
age = []
for age in final_ages:
if age == 'Unknown':
decade.append(age)
else:
decade.append(str(age))
for decade in decades:
decade=decade[:-1] + '0s'
decades.append(decade)
In addition, you could also give a more descriptive title and tag your topic so that other with similar issues as you can find an answer more quickly. Thanks for helping to make our community a better place!