0% found this document useful (0 votes)
8 views4 pages

S.C 2

This document describes a simple Parking Management System implemented in Python, which allows users to manage vehicle entries and exits, track parking slot availability, and save the status of slots in a CSV file. The system initializes parking slots, updates their status based on user input, and ensures that the number of vehicles does not exceed available slots. The script also maintains persistent data across runs by using a CSV file to store slot statuses.

Uploaded by

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

S.C 2

This document describes a simple Parking Management System implemented in Python, which allows users to manage vehicle entries and exits, track parking slot availability, and save the status of slots in a CSV file. The system initializes parking slots, updates their status based on user input, and ensures that the number of vehicles does not exceed available slots. The script also maintains persistent data across runs by using a CSV file to store slot statuses.

Uploaded by

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

Here's a simple Parking Management System implemented in

Python. It allows users to:

 Input the number of vehicles entering and leaving.


 Manage parking slots.
 Save and update the status of slots in a CSV file.
 Show slot availability.

📁 File: parking_management.py

import csv
import os

CSV_FILE = 'parking_slots.csv'
TOTAL_SLOTS = 10 # You can change this number

def initialize_slots():
if not os.path.exists(CSV_FILE):
with open(CSV_FILE, 'w', newline='') as
f:
writer = csv.writer(f)
writer.writerow(['Slot', 'Status'])
# Header
for i in range(1, TOTAL_SLOTS + 1):
writer.writerow([f'Slot{i}',
'Empty'])

def load_slots():
with open(CSV_FILE, 'r') as f:
reader = csv.DictReader(f)
return list(reader)

def save_slots(slots):
with open(CSV_FILE, 'w', newline='') as f:
writer = csv.DictWriter(f,
fieldnames=['Slot', 'Status'])
writer.writeheader()
writer.writerows(slots)

def show_available_slots(slots):
available = [slot for slot in slots if
slot['Status'] == 'Empty']
print(f"\nAvailable Slots:
{len(available)}")
for slot in available:
print(slot['Slot'])

def enter_vehicle(slots, count):


available = [slot for slot in slots if
slot['Status'] == 'Empty']
if count > len(available):
print("Not enough available slots!")
return slots

for i in range(count):
available[i]['Status'] = 'Occupied'

print(f"{count} vehicle(s) entered.")


return slots

def leave_vehicle(slots, count):


occupied = [slot for slot in slots if
slot['Status'] == 'Occupied']
if count > len(occupied):
print("Fewer vehicles in parking than
you want to remove.")
return slots

for i in range(count):
occupied[i]['Status'] = 'Empty'

print(f"{count} vehicle(s) left.")


return slots

def main():
initialize_slots()

while True:
slots = load_slots()
show_available_slots(slots)

try:
entering = int(input("\nNumber of
vehicles entering (0 to skip): "))
leaving = int(input("Number of
vehicles leaving (0 to skip): "))
except ValueError:
print("Please enter valid
numbers.")
continue

if entering < 0 or leaving < 0:


print("Negative values not
allowed.")
continue

if entering > 0:
slots = enter_vehicle(slots,
entering)
if leaving > 0:
slots = leave_vehicle(slots,
leaving)

save_slots(slots)
cont = input("\nContinue? (y/n):
").strip().lower()
if cont != 'y':
break

if __name__ == '__main__':
main()

📝 CSV File Example (after execution)


python-repl
CopyEdit
Slot,Status
Slot1,Occupied
Slot2,Empty
...

This script ensures that:

 It never allocates more vehicles than the available slots.


 Slot statuses are persistent between runs using a CSV file

You might also like