0% found this document useful (0 votes)
7 views5 pages

Pyth

Uploaded by

madmac653
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Pyth

Uploaded by

madmac653
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#!

/usr/bin/env python
# coding: utf-8

# In[ ]:

import pickle

# Data structure to store school records


schools = []

# Function to display sports categories


def display_sports():
print("\nSports Categories:")
print("1. Basketball")
print("2. Football")
print("3. Cricket")
print("4. Volleyball")
print("5. Tennis")
print("6. Badminton")
print("7. Throwball")
print()

# Function to add a school and their results


def add_school():
school_name = input("Enter the name of the school: ")
num_students = int(input(f"Enter number of students from {school_name}: "))
games_won = {}

# Display available sports and take input for each sport


display_sports()
games_won["Basketball"] = int(input("Enter number of Basketball games won: "))
games_won["Football"] = int(input("Enter number of Football games won: "))
games_won["Cricket"] = int(input("Enter number of Cricket games won: "))
games_won["Volleyball"] = int(input("Enter number of Volleyball games won: "))
games_won["Tennis"] = int(input("Enter number of Tennis games won: "))
games_won["Badminton"] = int(input("Enter number of Badminton games won: "))
games_won["Throwball"] = int(input("Enter number of Throwball games won: "))

# Calculate total games won


total_games_won = sum(games_won.values())

school_record = {
"school_name": school_name,
"num_students": num_students,
"games_won": games_won,
"total_games_won": total_games_won
}

# Append the school record to the schools list


schools.append(school_record)
print(f"\nRecord for {school_name} added successfully!\n")

# Function to display the rankings based on games won


def display_ranks():
if not schools:
print("No school records available.")
return
# Sort schools based on the total games won (descending order)
sorted_schools = sorted(schools, key=lambda x: x["total_games_won"],
reverse=True)

print("\nSchool Rankings Based on Games Won:")


print("Rank\tSchool Name\tTotal Games Won")
for rank, school in enumerate(sorted_schools, start=1):
print(f"{rank}\t{school['school_name']}\t\t{school['total_games_won']}")
print()

# Function to save the records to a file


def save_records():
with open('school_sports_records.dat', 'wb') as f:
pickle.dump(schools, f)
print("\nRecords saved successfully!\n")

# Function to load the records from the file


def load_records():
global schools
try:
with open('school_sports_records.dat', 'rb') as f:
schools = pickle.load(f)
print("\nRecords loaded successfully!\n")
except FileNotFoundError:
print("\nNo previous records found, starting fresh.\n")

# Main Menu function


def homepage():
while True:
print("\nSports Management System")
print("1. Add School and Results")
print("2. Display Rankings")
print("3. Save Records")
print("4. Load Records")
print("5. Exit")
option = int(input("Enter your option: "))

if option == 1:
add_school()
elif option == 2:
display_ranks()
elif option == 3:
save_records()
elif option == 4:
load_records()
elif option == 5:
print("Exiting the program. Goodbye!")
break
else:
print("Invalid option, please try again.")

# Start the program


homepage()
import pymysql

# Connect to MySQL database


conn = pymysql.connect(
host="localhost",
user="root",
passwd="123",
db="sports_management"
)

# Function to display sports categories


def display_sports():
print("\nSports Categories:")
print("1. Basketball")
print("2. Football")
print("3. Cricket")
print("4. Volleyball")
print("5. Tennis")
print("6. Badminton")
print("7. Throwball")
print()

# Function to add a school and their results


def add_school():
school_name = input("Enter the name of the school: ")
num_students = int(input(f"Enter number of students from {school_name}: "))

# Insert school data into the `schools` table


cur = conn.cursor()
insert_school_query = """
INSERT INTO schools (school_name, num_students)
VALUES (%s, %s);
"""
cur.execute(insert_school_query, (school_name, num_students))
conn.commit()

# Get the school ID for the newly added school


school_id = cur.lastrowid

games_won = {}
display_sports()
games_won["Basketball"] = int(input("Enter number of Basketball games won: "))
games_won["Football"] = int(input("Enter number of Football games won: "))
games_won["Cricket"] = int(input("Enter number of Cricket games won: "))
games_won["Volleyball"] = int(input("Enter number of Volleyball games won: "))
games_won["Tennis"] = int(input("Enter number of Tennis games won: "))
games_won["Badminton"] = int(input("Enter number of Badminton games won: "))
games_won["Throwball"] = int(input("Enter number of Throwball games won: "))

total_games_won = sum(games_won.values())

# Insert sports results into the `sports_results` table


insert_results_query = """
INSERT INTO sports_results (school_id, basketball, football, cricket,
volleyball, tennis, badminton, throwball, total_games_won)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s);
"""
cur.execute(insert_results_query, (school_id, games_won["Basketball"],
games_won["Football"],
games_won["Cricket"],
games_won["Volleyball"], games_won["Tennis"],
games_won["Badminton"],
games_won["Throwball"], total_games_won))
conn.commit()
print(f"\nRecord for {school_name} added successfully!\n")
cur.close()

# Function to display the rankings based on games won


def display_ranks():
cur = conn.cursor()
query = """
SELECT s.school_name, sr.total_games_won
FROM schools s
JOIN sports_results sr ON s.school_id = sr.school_id
ORDER BY sr.total_games_won DESC;
"""
cur.execute(query)
results = cur.fetchall()

print("\nSchool Rankings Based on Games Won:")


print("Rank\tSchool Name\tTotal Games Won")
rank = 1
for result in results:
print(f"{rank}\t{result[0]}\t\t{result[1]}")
rank += 1
cur.close()

# Function to save records (to the database in this case, already done during
insertion)
def save_records():
print("\nRecords are saved to the database!\n")

# Function to load records (from the database)


def load_records():
print("\nRecords are loaded from the database!\n")

# Main Menu function


def homepage():
while True:
print("\nSports Management System")
print("1. Add School and Results")
print("2. Display Rankings")
print("3. Save Records")
print("4. Load Records")
print("5. Exit")
option = int(input("Enter your option: "))

if option == 1:
add_school()
elif option == 2:
display_ranks()
elif option == 3:
save_records()
elif option == 4:
load_records()
elif option == 5:
print("Exiting the program. Goodbye!")
break
else:
print("Invalid option, please try again.")

# Start the program


homepage()
# Close the MySQL connection when done
conn.close()

# In[ ]:

You might also like