0% found this document useful (0 votes)
3 views7 pages

Cs Practical Python Answer

The document contains three separate Python programs for managing records: one for shoe records using pickle, one for stack operations, and another for World Cup team records using CSV. Each program features a menu-driven interface allowing users to add, display, and search records. Error handling is implemented for file operations and user inputs.

Uploaded by

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

Cs Practical Python Answer

The document contains three separate Python programs for managing records: one for shoe records using pickle, one for stack operations, and another for World Cup team records using CSV. Each program features a menu-driven interface allowing users to add, display, and search records. Error handling is implemented for file operations and user inputs.

Uploaded by

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

1) .

import pickle

FILE = "[Link]"

def add_record():

f = open(FILE, "ab")

s_id = int(input("Enter Shoe ID: "))

name = input("Enter Shoe Name: ")

brand = input("Enter Brand: ")

typ = input("Enter Type: ")

price = float(input("Enter Price: "))

record = [s_id, name, brand, typ, price]

[Link](record, f)

[Link]()

print("Record added successfully!\n")

def display_records():

try:

f = open(FILE, "rb")

print("\nAll Shoe Records:")

while True:

try:

rec = [Link](f)

print(rec)

except EOFError:

break
[Link]()

except FileNotFoundError:

print("File not found!\n")

def search_record():

try:

f = open(FILE, "rb")

sid = int(input("Enter Shoe ID to search: "))

found = False

while True:

try:

rec = [Link](f)

if rec[0] == sid:

print("Record found:", rec)

found = True

break

except EOFError:

break

if not found:

print("Record not found!")

[Link]()

except FileNotFoundError:

print("File not found!\n")

while True:
print("\n===== MENU =====")

print("1. Add record")

print("2. Display records")

print("3. Search record")

print("4. Exit")

choice = int(input("Enter your choice: "))

if choice == 1:

add_record()

elif choice == 2:

display_records()

elif choice == 3:

search_record()

elif choice == 4:

print("Program ended.")

break

else:

print("Invalid choice!")

2).

stack = []

while True:

print("\n----- STACK MENU -----")

print("1. Push")

print("2. Pop")

print("3. Display")

print("4. Exit")
choice = int(input("Enter your choice: "))

if choice == 1:

item = input("Enter element to push: ")

[Link](item)

print(item, "pushed into stack")

elif choice == 2:

if stack == []:

print("Stack is empty! Cannot pop.")

else:

print("Popped element:", [Link]())

elif choice == 3:

if stack == []:

print("Stack is empty!")

else:

print("Stack elements are:")

for i in reversed(stack):

print(i)

elif choice == 4:

print("Program ended.")

break

else:

print("Invalid choice!")

3. import csv
FILE = "[Link]"

def add_record():

f = open(FILE, "a", newline="")

w = [Link](f)

team_id = int(input("Enter Team ID: "))

team = input("Enter Team Name: ")

wins = int(input("Enter Wins: "))

lost = int(input("Enter Lost: "))

titles = int(input("Enter Titles: "))

[Link]([team_id, team, wins, lost, titles])

[Link]()

print("Record added successfully!\n")

def display_records():

try:

f = open(FILE, "r")

r = [Link](f)

print("\nAll Team Records:")

for row in r:

print(row)

[Link]()

except FileNotFoundError:

print("File not found!\n")

def search_record():
try:

f = open(FILE, "r")

r = [Link](f)

team_name = input("Enter team name to search: ")

found = False

for row in r:

if row[1].lower() == team_name.lower():

print("Record Found:", row)

found = True

break

if not found:

print("Record not found!")

[Link]()

except FileNotFoundError:

print("File not found!\n")

while True:

print("\n===== MENU =====")

print("1. Add record")

print("2. Display records")

print("3. Search record (By Team)")

print("4. Exit")

choice = int(input("Enter your choice: "))

if choice == 1:

add_record()
elif choice == 2:

display_records()

elif choice == 3:

search_record()

elif choice == 4:

print("Program ended.")

break

else:

print("Invalid choice!")

You might also like