Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

PYTHON TUPLE SCENARIO BASED PROGRAMS WITH PROGRAM

1. Student Records:
Write a program that stores student records using tuples. Each tuple should contain
information like student name, roll number, and marks in three subjects. Allow users to
add new records, display all records, and search for a student record by roll number.

def add_record(records, name, roll_number, marks):


records.append((name, roll_number, marks))
print("Record added successfully.")

def display_records(records):
print("Student Records:")
for record in records:
print(f"Name: {record[0]}, Roll Number: {record[1]}, Marks: {record[2]}")

def search_record(records, roll_number):


for record in records:
if record[1] == roll_number:
print(f"Record found: Name - {record[0]}, Roll Number - {record[1]}, Marks -
{record[2]}")
return
print("Record not found.")

def main():
student_records = []
while True:
print("\n1. Add Record")
print("2. Display Records")
print("3. Search Record")
print("4. Exit")
choice = input("Enter your choice: ")

if choice == "1":
name = input("Enter student name: ")
roll_number = input("Enter roll number: ")
marks = tuple(map(int, input("Enter marks in three subjects separated by commas:
").split(',')))
add_record(student_records, name, roll_number, marks)
elif choice == "2":
display_records(student_records)
elif choice == "3":
roll_number = input("Enter roll number to search: ")
search_record(student_records, roll_number)
elif choice == "4":
break
else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main()

______________________________________________________________________________
2. Employee Details:
Create a program to manage employee details using tuples. Each tuple should contain
information like employee name, employee ID, and salary. Allow users to add new
employees, display all employee details, and search for an employee by ID.
def add_employee(employees, name, emp_id, salary):
employees.append((name, emp_id, salary))
print("Employee added successfully.")

def display_employees(employees):
print("Employee Details:")
for employee in employees:
print(f"Name: {employee[0]}, Employee ID: {employee[1]}, Salary: {employee[2]}")

def search_employee(employees, emp_id):


for employee in employees:
if employee[1] == emp_id:
print(f"Employee found: Name - {employee[0]}, Employee ID - {employee[1]}, Salary -
{employee[2]}")
return
print("Employee not found.")

def main():
employee_details = []
while True:
print("\n1. Add Employee")
print("2. Display Employees")
print("3. Search Employee")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == "1":
name = input("Enter employee name: ")
emp_id = input("Enter employee ID: ")
salary = float(input("Enter employee salary: "))
add_employee(employee_details, name, emp_id, salary)
elif choice == "2":
display_employees(employee_details)
elif choice == "3":
emp_id = input("Enter employee ID to search: ")
search_employee(employee_details, emp_id)
elif choice == "4":
break
else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main()

______________________________________________________________________________

You might also like