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
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'
–
–
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.