0% found this document useful (0 votes)
11 views

Code2pdf 6743fbf155ede

code computer
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)
11 views

Code2pdf 6743fbf155ede

code computer
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
You are on page 1/ 4

import pickle

# TO CREATE A FUNCTION THAT ENTERS THE DETAILS OF CARS.


def create_car():
with open("maruticar.dat", "ab") as f:
while True:
car = {}
print("Enter details of car:")
car["name"] = input("Name: ").strip()
car["length"] = input("length: ").strip()
car["width"] = input("width: ").strip()
car["height"] = input("height: ").strip()
car["seating capacity"] = input("seating capacity: ").
car["engine type"] = input("engine type: ").strip()
car["capacity"] = input("capacity: ").strip()
car["power"] = input("power: ").strip()
car["torque"] = input("torque: ").strip()
car["fuel tank capacity"] = input("fuel tank capacity: "
car["price"] = input("price: ").strip()
pickle.dump(car, f)
ans= input("Add another? (y/n): ").strip()
if ans not in["y","yes","Y"]:
break

# TO DISPLAY THE RECORDS ENTERED


def display_cars():
try:
with open("maruticar.dat", "rb") as f:
print("Car Records:")
while True:
car = pickle.load(f)
print(car)
except EOFError:
pass
except FileNotFoundError:
print("No car records found.")

# TO SEARCH FOR ANY RECORD ENTERED


def search_car():
try:
with open("maruticar.dat", "rb") as f:
name = input("Enter car name to search: ").strip()
found = False
while True:
car = pickle.load(f)
if car["name"].lower() == name.lower():
print("Car found:")
print(car)
found = True
break
except EOFError:
if not found:
print("Car not found.")
except FileNotFoundError:
print("No car records found.")

# TO UPDATE ANY DETAIL OF RECORD ENTERED


def update_car_information():
try:
with open("maruticar.dat", "rb") as f:
cars = []
while True:
cars.append(pickle.load(f))
except EOFError:
pass
except FileNotFoundError:
print("No car records found.")
return

name = input("Enter car name to update: ").strip()


found = False

for car in cars:


if car["name"].lower() == name.lower():
print("Car found:")
print(car)
print("1. Name\n2. length\n3. width\n4. height\n5. seating
print("8. power\n9. torque\n10. fuel tank capacity\n11. pri
choice = input("What to update? (Enter the number): ")
if choice in["name","Name","NAME"]:
car["name"] = input("Enter new name: ").strip()
elif choice in["length","Length","LENGTH"]:
car["length"] = input("Enter new length: ").strip
elif choice in["width","Width","WIDTH"]:
car["width"] = input("Enter new width: ").strip()
elif choice in["height","Height","HEIGHT"]:
car["height"] = input("Enter new height: ").strip()
elif choice in["seating capacity","Seating capacity","Seati
car["seating capacity"] = input("Enter new seating capa
elif choice in["engine type","Engine type","Engine Type"
car["engine type"] = input("Enter new engine type: "
elif choice in["capacity","Capacity","CAPACIITY"]:
car["capacity"] = input("Enter new capacity: ").strip
elif choice in["power","Power","POWER"]:
car["power"] = input("Enter new power: ").strip()
elif choice in["torque","Torque","TORQUE"]:
car["torque"] = input("Enter new torque: ").strip()
elif choice in["fuel tank capacity","Fuel tank capacity"
car["fuel tank capacity"] = input("Enter new fuel tank
elif choice in["prize","Prize","PRIZE"]:
car["prize"] = input("Enter new prize: ").strip()
else:
print("Invalid choice. No changes made.")
return
print("Car details updated successfully.")
found = True
break

if not found:
print("Car not found.")

with open("maruticar.dat", "wb") as f:


for car in cars:
pickle.dump(car, f)

# TO DELETE THE RECORD ENTERED BY USER


def delete_car():
try:
with open("maruticar.dat", "rb") as f:
cars = []
while True:
cars.append(pickle.load(f))
except EOFError:
pass
except FileNotFoundError:
print("No car records found.")
return

name = input("Enter car name to delete: ").strip()


found = False

for i in range(len(cars)):
if cars[i]["name"].lower() == name.lower():
print("Car found:")
print(cars[i])
del cars[i]
print("Car deleted successfully.")
found = True
break

if not found:
print("Car not found.")

with open("maruticar.dat", "wb") as f:


for car in cars:
pickle.dump(car, f)

# TO CALL THE FUNCTION AND USE THEM AS PER THEIR FUNCTIONS


def main():
while True:
print("\nMenu:")
print("1. Add car")
print("2. Display cars")
print("3. Search car")
print("4. Update car")
print("5. Delete car")
print("6. Exit")
try:
choice = input("Enter your choice from menu : ")
if choice in["add car","Add car","Add Car","ADD CAR","1"
create_car()
elif choice in["display cars","Display cars","Display Cars"
display_cars()
elif choice in["search car","Search car","Search Car",
search_car()
elif choice in ["update cat","Update car","Update Car"
update_car_information()
elif choice in ["delete car","Delete car","Delete Car"
delete_car()
elif choice in["exit","Exit","EXIT","6"]:
print("Exiting.")
break
else:
print("Invalid choice. Please try again.")
except ValueError:
print("Invalid input. Please enter your choice from menu on

main()

You might also like