Python Data Analysis Basics - Calculating Artist Ages

Hi,

I definitely did the right coding but still the platform doesnt give me the correct answer. this frustrates me as i cannot validate or do any of the next of the exercises

below is the code :slight_smile:
from csv import reader

Read the artworks_clean.csv file

opened_file = open(‘artworks_clean.csv’)
read_file = reader(opened_file)
moma = list(read_file)
moma = moma[1:]

ages =
for row in moma:
birth = row[3]
date = row[6]
if type(birth) == int:
age = date - birth
else:
age = 0
ages.append(age)

final_ages =
for age in ages:
if age > 20:
final_age = age
else:
final_age = “Unknown”
final_ages.append(final_age)

print(final_ages)

it should display ages of the list. instead its displaying as below :slight_smile:

[‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’, ‘Unknown’,

becuase of this issue, i could not complete any of the 11 exercizes in this unit

Hey @san.sap201,

  • First thing to check here is, if the datatype of row[3] and row[6] is int or not. Just below the code line birth = row[3], write print(type(birth)) and below the code line date = row[6], write print(type(birth)) - if the result is int, that means you have already converted both birth and date into int. if not, first try to convert it into int.

  • After the 1st step, there is possibly one issue that i could see, are you try to append variable age to ages list in the else block ?? because the indentation is not visible here hence i am asking this question.

append age in the ages list after the if else block, like this.

if type(birth) is int:
    age = date - birth
else:
    age = 0
ages.append(age)

I think this should solve the issue.

Thanks!

Hi @rksiita,

I tried to check the type, its coming as string.

the values are as below :

isdigit() also returned true

then i tried to convert string to int as below:
ages =
for row in moma:
date = row[6]
birth = row[3]
if birth != “”:
birth = int(birth)

date = int(date)
if type(birth) == int:
    age = date - birth
else:
    age = 0
ages.append(age)

final_ages =
for row in ages:
if row > 20:
final_age = row
else:
final_age = “Unknown”
final_ages.append(final_age)
print(final_ages)

i was able to get the output

thanks

Hey, @san.sap201,

Glad that it worked out. But you missed to mention one condition in the code.

As i said in my first post that, you have to convert both row[3] and row[6] values into int from string.
I see what you have done, you have added one if loop, that checks if the birth_date is not blank, then convert both birth_date and date into int from string. But we are not sure that if the value for birth date is present then the value for date (row[6]) is also present. There might be a case, that when birth date is given but date is not given or vice-versa. In this case your if loop will fail. Hence, you should write a separate if loop for converting birth date and date values.

Thanks.
Happy learning buddy.

1 Like

Ya @rksiiita. I tried all the things but missed to check the points . Glad you mentioned those.

Thanks again !!

Cool man. Cheers :+1: