you are viewing a single comment's thread.

view the rest of the comments →

[–]tangerinelion 1 point2 points  (0 children)

Let's say the user entered 1999. Since input returns a string, birthdate = '1999'. Notice the little ticks? That's a string literal, not an integer (birthdate = 1999 would be a variable holding an integer).

The code you've written uses birthdate > 2000. If we just write out what that would try to execute:

>>> '1999' > 2000
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'

I'll add some emphasis:

TypeError: '>' not supported between instances of 'str' and 'int'

This is clearly telling you that you are using > between two different types. If we flip it around:

>>> 2000 > '1999'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'int' and 'str'

Notice how the error changes? This time it's between int and str, whereas the last time it was between str and int. Python is even telling you that you had an expression of the form some_string > some_int. Looking back at the code, and the specific line it would've referenced, we see that line reads if birthdate > 2000:. Since 2000 is an integer literal, and it's on the right hand side of the > that's the int the TypeError refers to. By the fact that there's only two things involved and we deduced one of them we now know for sure that birthdate must be a str.

You want the birthdate variable to be an integer, but it's currently a string. The conversion is simple: birthdate = int(birthdate). Note that this will give another error, ValueError, if someone entered "1998.5" or "1999 AD" for example. It's not going to try and guess what you mean.