Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Converting Java to Python and I keep getting TypeError: '<=' not supported between instances of 'list' and 'int'

Ask Question

Hello I'm new to python and java and I'm currently creating a function that validates a person's birthday, if it's following the proper format and if they're of legal age.

This is the java code:

boolean MM = false;
        boolean DD = false;
        boolean YY = false;
        String birthday;
        birthday = textFieldBirthday.getText();
        int month = Integer.parseInt(birthday.split("/")[0]);
        int day = Integer.parseInt(birthday.split("/")[1]);
        int year = Integer.parseInt(birthday.split("/")[2]);
            if (day <= 31){
                lblBirthdayFormat.setForeground(Color.green);
                lblBirthdayFormat.setText("Valid Date");
                DD = true;
            else{
                lblBirthdayFormat.setForeground(Color.red);
                lblBirthdayFormat.setText("Invalid Date");
            if (month <= 12 && month > 00 && DD == true){
                lblBirthdayFormat.setForeground(Color.green);
                lblBirthdayFormat.setText("Valid Date");
                MM = true;
            else{
                lblBirthdayFormat.setForeground(Color.red);
                lblBirthdayFormat.setText("Invalid Date");
            if (year - 0 <= 04 || year - 0 > 23 && DD == true && MM == true){
                lblBirthdayFormat.setForeground(Color.green);
                lblBirthdayFormat.setText("Valid Date");
                YY = true;
            else{
                lblBirthdayFormat.setForeground(Color.red);
                lblBirthdayFormat.setText("Invalid Date");
            if (MM == true && DD == true && YY == true){
                lblBirthdayFormat.setForeground(Color.green);
                lblBirthdayFormat.setText("Valid Date");
            else{
                lblBirthdayFormat.setForeground(Color.red);
                lblBirthdayFormat.setText("Invalid Date");
            if (DD == true && MM == true && YY == false){
                lblBirthdayFormat.setForeground(Color.red);
                lblBirthdayFormat.setText("Minors are not allowed.");

This is my python code:

def keyUpBday(e):
    global MM
    global DD
    global YY
    birthday = customerBirthday.get()
    month = (birthday.split("/"))
    day = (birthday.split("/"))
    year = (birthday.split("/"))
    if day <= 31:
        labelBirthday.config(text = "Valid Date")
        DD = True
    else:
        labelBirthday.config(text = "Invalid Date")
    if month <= 12 and month > 00 and DD == True:
        labelBirthday.config(text = "Valid Date")
        MM = True
    else:
        labelBirthday.config(text = "Invalid Date")
    if year - 0 <= 4 or year - 0 > 23 and DD == True and MM == True:
        labelBirthday.config(text = "Valid Date")
        YY = True
    else:
        labelBirthday.config(text = "Invalid Date")
    if MM == True and DD == True and YY == True:
        labelBirthday.config(text = "Valid Date")
    else:
        labelBirthday.config(text = "Invalid Date")
    if MM == True and DD == True and YY == False:
         labelBirthday.config(text = "Minors are not allowed.")

This is the error I get: Exception in Tkinter callback Traceback (most recent call last): File "C:\Python39\lib\tkinter_init_.py", line 1892, in call return self.func(*args) File "C:\Users\Admin\OneDrive\Documents\Acads\CompProg2\Prog2Exer2.py", line 37, in keyUpBday if day <= 31: TypeError: '<=' not supported between instances of 'list' and 'int'

.split() -ing a string returns a list of strings, you probably want to get a specific parts by using indexing month = birthday.split("/")[0] if month is the first part in your date. Might fail if the user inputs a bad format. Also consider assigning the split result a variable so you don't have to do it 3 times. – ivvija Mar 10 at 12:10 Your variables month, day and year are all identical lists. Also, why year - 0 ? What will happen next year (2024)? You'll need to change the code. Probably not the best approach. Take a look at the datetime module for useful ways to parse/validate the date string – DarkKnight Mar 10 at 13:05

The datetime module has functions that will enable you to validate a date string according to some well-known format - in your case the bizarre American format.

Using the right tools you can make this much simpler, more robust and future-proof (your current code would have to be changed after 2023).

Something like this (assumes minors are under 18):

from datetime import datetime
from dateutil.relativedelta import relativedelta
def keyUpBday(e):
        _date = datetime.strptime(customerBirthday.get(), '%m/%d/%y')
        _now = datetime.now()
        _text = 'Valid date' if relativedelta(_now, _date).years >= 18 else 'Minors are not allowed'
        labelBirthday.config(text=_text)
    except ValueError:
        labelBirthday.config(text='Invalid Date')
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.