LIMITED TIME OFFER: 50% OFF OF PREMIUM WITH OUR ANNUAL PLAN (THAT'S $294 IN SAVINGS).
GET OFFER

I'm not sure why my checking type(date) ==0 isn't working? and giving me an error, isn't it same as checking type(birth_date) ==0?

Screen Link:

https://app.dataquest.io/m/331/python-data-analysis-basics/2/calculating-artist-ages

My Code:

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

Code given in answer:

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

What I expected to happen:

I’m getting an error when I’m checking type(date) == 0 isn’t it same as checking type(birth_date ==0), could someone help me why i’m getting an error?

What actually happened:
error when using type(date) ==0 instead of type(birth_date ==0)
Replace this line with the output/error

TypeErrorTraceback (most recent call last)
<ipython-input-1-c209a7294056> in <module>()
      4     date =  row[6]
      5     if type(date) == int:
----> 6         artist_age = (date) - (birth_date)
      7         ages.append(artist_age)
      8     else:

TypeError: unsupported operand type(s) for -: 'int' and 'str' 

let me know why i’m gettig an error

1 Like

It returns the same thing both time data-type of a given object.

You are getting TypeError as one of the operand is string type. (Probably birth_date is string. As you removed check for that.) And operation between string and int is not possible so.

birth_date and date must be int type.

yeah, a few values in the datset are str which I couldn’t convert to int since they were blank. hence I guess the operation is not working and even when I try birthdate = int(row[3]) it isn’t working. it’s probably because a few elements in row[3] are blank “”

Hi @sakethsai32,

Yes, some of the data in the birth_date values are empty strings. And that is why the instruction directed us to do this:

  • If the birth date is an int , calculate the age of the artist at the time of creating the artwork, and assign it to the variable age .
  • If birth isn’t an int type, assign 0 to the variable age .

You are getting an error here because you are checking the type of date variable instead of the birth_date variable.

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

Because all of the values in the date column (row[6]) are integer, it passes the type(date) == int: condition. But the problem occurs in the next line.

artist_age = (date) - (birth_date)

date is an integer but birth_date is a string. That is why you are getting the TypeError: unsupported operand type(s) for -: 'int' and 'str' .

To fix this issue, just replace if type(date) == int: with if type(birth_date) == int:

Best,
Sahil

2 Likes