Why not? Please explain

Screen Link: https://app.dataquest.io/m/331/python-data-analysis-basics/3/converting-ages-to-decades

My Code:

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)

What I expected to happen:

What actually happened:

Replace this line with the output/error

Hi @yiiiija next time please check these guidelines before posting a technical question

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.

Hi @jithins123. Thanks for quick replay and apologizes for my not friendly post… :relaxed: its my first post…

I understand my code, so then, why don’t pass through? (task 331.3 on “Python Data Analysis Basic”)

Thank you!

Hi @yiiiija,

This is your last for loop that is wrong. Try:

for d in decade:
   _str = d[:-1] + '0s'
   decades.append(_str)

Now with your loop, ‘0s’ is added to all [decades]… which is wrong…

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 !

Thank you again. I did refer to the specific task on mi 2 previous message:

Please, tell me. Why my code (or yours) doesnt work in the indicated task ?

It’s basically impossible to find the course like that. Please open the course and just provide the link of your browser.

Hi @yiiiija,

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)

Why, by the way, do you need 2 different lists decade and decades? It will be more than enough only one. Try this:

decades = []
for age in final_ages:
    if age == "Unknown":
        decade = age
    else:
        decade = str(age)[:-1] + "0s"
    decades.append(decade)
1 Like

I got it with the link to the course:

decades = []

age = []
for age in final_ages:
    if age == 'Unknown':
        decade='Unknown'
    else:
        decade=str(age)[:-1] + "0s"
    decades.append(decade)

Indeed in the course instructions it’s said decade should be a variable, not a list. No need to have 2 lists here.

1 Like

Thank you very much, Im newly, will try more…

1 Like

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!

2 Likes