Corrected Code Document Updated
Corrected Code Document Updated
Practical Question 1
Python Program for Stack Operations:
class BookStack:
def __init__(self):
self.stack = []
def pop(self):
if not self.stack:
print("The stack is empty. Nothing to pop.")
else:
removed_book = self.stack.pop()
print(f'"{removed_book}" has been removed from the stack.')
def traverse(self):
if not self.stack:
print("The stack is empty.")
else:
print("Books in the stack:")
for book in reversed(self.stack):
print(book)
# Menu-driven interface
stack = BookStack()
while True:
print("\nMenu:\n1. Push a Book\n2. Pop a Book\n3. Traverse Stack\n4. Exit")
try:
choice = int(input("Enter your choice: "))
if choice == 1:
book_name = input("Enter the name of the book: ")
stack.push(book_name)
elif choice == 2:
stack.pop()
elif choice == 3:
stack.traverse()
elif choice == 4:
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")
except ValueError:
print("Invalid input. Please enter a valid number.")
Practical Question 2
SQL Commands for Books Table:
Practical Question 3
Python Program for Bank Details Storage in Text File:
bank_details = []
n = int(input("Enter the number of bank records: "))
for _ in range(n):
accno = input("Enter Account Number: ")
name = input("Enter Name: ")
bal = input("Enter Balance: ")
bank_details.append(f"{accno},{name},{bal}\n")
with open("bank.txt", "w") as file:
file.writelines(bank_details)
Practical Question 4
SQL Commands for Train and Reservation Tables:
Practical Question 5
Python Program for Displaying Books with Price Greater Than 500 Using MySQL:
import mysql.connector
try:
# Establishing connection
conn = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="lib"
)
cursor = conn.cursor()
# Displaying results
print("Books with price greater than 500:")
for row in cursor.fetchall():
print(row)
except mysql.connector.Error as e:
print(f"Error: {e}")
finally:
if conn.is_connected():
cursor.close()
conn.close()
Practical Question 6
SQL Commands for Fee Table:
-- Create the fee table
CREATE TABLE fee (
studid INT,
studname CHAR(10),
stud_fee INT
);
Practical Question 7
Python Program for Loan Details Storage in a Binary File:
import pickle
loan_details = []
n = int(input("Enter the number of loan records: "))
for _ in range(n):
loanno = int(input("Enter Loan Number: "))
name = input("Enter Name: ")
loan_amt = float(input("Enter Loan Amount: "))
loan_details.append({"loanno": loanno, "name": name, "loan_amt": loan_amt})