SOURCE CODE
import re
from datetime import datetime
# Store all candidate registrations in memory
registrations = []
# ---------- VALIDATORS ----------
def validate_name(name, label="Name"):
if not name.replace(" ", "").isalpha():
raise ValueError(f"❌ {label} must contain only alphabets and spaces.")
return name.strip().title()
def validate_dob(dob_str):
try:
dob = datetime.strptime(dob_str, "%d-%m-%Y")
today = datetime.today()
age = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day))
return dob_str, age
except ValueError:
raise ValueError("❌ DOB must be in format DD-MM-YYYY (e.g., 15-08-2000).")
def validate_email(email):
pattern = r'^[\w\.-]+@[\w\.-]+\.\w{2,3}$'
if not re.match(pattern, email.strip()):
raise ValueError("❌ Invalid email format.")
return email.strip()
def validate_mobile(mobile):
if not re.match(r'^[6-9]\d{9}$', mobile.strip()):
raise ValueError("❌ Mobile number must be 10 digits starting with 6-9.")
return mobile.strip()
def validate_gender(gender):
gender = gender.strip().lower()
if gender in ['m', 'male']:
return 'Male'
elif gender in ['f', 'female']:
return 'Female'
elif gender in ['o', 'other']:
return 'Other'
else:
raise ValueError("❌ Gender must be Male (m), Female (f), or Other (o).")
def validate_category(category):
category = category.strip().lower()
category_map = {
'g': 'GENERAL',
'general': 'GENERAL',
'o': 'OBC', 'obc': 'OBC','s': 'SC','sc': 'SC', 't': 'ST', 'st': 'ST','e': 'EWS','ews': 'EWS'
if category in category_map:
return category_map[category]
else:
raise ValueError("❌ Category must be: General (g), OBC (o), SC (s), ST (t), or EWS (e).")
# ---------- REGISTRATION ----------
def register_candidate():
print("\n--- New Candidate Registration ---")
while True:
try:
name = validate_name(input("Full Name: "))
break
except ValueError as e:
print(e)
while True:
try:
father = validate_name(input("Father's Name: "), "Father's Name")
break
except ValueError as e:
print(e)
while True:
try:
mother = validate_name(input("Mother's Name: "), "Mother's Name")
break
except ValueError as e:
print(e)
while True:
try:
dob_str, age = validate_dob(input("Date of Birth (DD-MM-YYYY): "))
break
except ValueError as e:
print(e)
while True:
try:
gender = validate_gender(input("Gender (Male/Female/Other or M/F/O): "))
break
except ValueError as e:
print(e)
while True:
try:
category = validate_category(input("Category (General/OBC/SC/ST/EWS or G/O/S/T/E): "))
break
except ValueError as e:
print(e)
while True:
try:
email = validate_email(input("Email ID: "))
break
except ValueError as e:
print(e)
while True:
try:
mobile = validate_mobile(input("Mobile Number: "))
break
except ValueError as e:
print(e)
address = input("Full Address: ").strip()
candidate = {
"Name": name,
"Father": father,
"Mother": mother,
"DOB": dob_str,
"Age": age,
"Gender": gender,
"Category": category,
"Email": email,
"Mobile": mobile,
"Address": address
registrations.append(candidate)
print("\n✅ Registration Successful!")
print_registration_slip(candidate)
# ---------- PRINT SLIP ----------
def print_registration_slip(data):
print("\n🧾 --- Registration Slip ---")
for key, value in data.items():
print(f"{key}: {value}")
print("------------------------------")
# ---------- VIEW & SEARCH ----------
def view_all():
if not registrations:
print("⚠️No registrations yet.")
return
for i, reg in enumerate(registrations, 1):
print(f"\nCandidate {i}:")
for k, v in reg.items():
print(f"{k}: {v}")
def search_by_name():
name = input("Enter name to search: ").strip().title()
found = False
for reg in registrations:
if reg["Name"] == name:
print_registration_slip(reg)
found = True
if not found:
print("❌ Candidate not found.")
# ---------- ADMIN LOGIN ----------
def admin_login():
print("\n🔐 Admin Login")
username = input("Enter username: ")
password = input("Enter password: ")
if username == "admin" and password == "1234":
print("✅ Login successful.")
return True
else:
print("❌ Invalid credentials.")
return False
# ---------- MAIN MENU ----------
def main_menu():
if not admin_login():
return
while True:
print("\n========= UPSC Registration System =========")
print("1. Register a new candidate")
print("2. View all registrations")
print("3. Search candidate by name")
print("4. Exit")
print("============================================")
choice = input("Enter your choice (1-4): ").strip()
if choice == '1':
register_candidate()
elif choice == '2':
view_all()
elif choice == '3':
search_by_name()
elif choice == '4':
print("👋 Exiting the system.")
break
else:
print("❌ Invalid choice. Please select 1 to 4.")
# ---------- START PROGRAM ----------
if __name__ == "__main__":
main_menu()
OUTPUT
🔐 Admin Login
Enter username: admin
Enter password: 1234
✅ Login successful.
========= UPSC Registration System =========
1. Register a new candidate
2. View all registrations
3. Search candidate by name
4. Exit
============================================
Enter your choice (1-4): 1
--- New Candidate Registration ---
Full Name: hardik gaur
Father's Name: kranti gaur
Mother's Name: kamla gaur
Date of Birth (DD-MM-YYYY): 29-11-2007
Gender (Male/Female/Other or M/F/O): m
Category (General/OBC/SC/ST/EWS or G/O/S/T/E): g
Mobile Number: 8800647260
Full Address: 384 jagari vihar
✅ Registration Successful!
🧾 --- Registration Slip ---
Name: Hardik Gaur
Father: Kranti Gaur
Mother: Kamla Gaur
DOB: 29-11-2207
Age: -183
Gender: Male
Category: GENERAL
Email: [email protected]
Mobile: 8800647260
Address: 384 jagari vihar
------------------------------
========= UPSC Registration System =========
1. Register a new candidate
2. View all registrations
3. Search candidate by name
4. Exit
============================================
Enter your choice (1-4): 2
Candidate 1:
Name: Hardik Gaur
Father: Kranti Gaur
Mother: Kamla Gaur
DOB: 29-11-2207
Age: -183
Gender: Male
Category: GENERAL
Email: [email protected]
Mobile: 8800647260
Address: 384 jagari vihar
========= UPSC Registration System =========
1. Register a new candidate
2. View all registrations
3. Search candidate by name
4. Exit
============================================
Enter your choice (1-4): 3
Enter name to search: hardik gaur
🧾 --- Registration Slip ---
Name: Hardik Gaur
Father: Kranti Gaur
Mother: Kamla Gaur
DOB: 29-11-2207
Age: -183
Gender: Male
Category: GENERAL
Email: [email protected]
Mobile: 8800647260
Address: 384 jagari vihar
------------------------------
========= UPSC Registration System =========
1. Register a new candidate
2. View all registrations
3. Search candidate by name
4. Exit
============================================
Enter your choice (1-4): 4
👋 Exiting the system.
=== Code Execution Successful ===