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

Sample code

The document presents a simplified Parking Management System implemented in Python, which uses a CSV file to track parking slot statuses. It allows for vehicle entry and exit while displaying available slots. The code includes functions for initializing, loading, saving, and managing the parking slots.

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 views5 pages

Sample code

The document presents a simplified Parking Management System implemented in Python, which uses a CSV file to track parking slot statuses. It allows for vehicle entry and exit while displaying available slots. The code includes functions for initializing, loading, saving, and managing the parking slots.

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/ 5

Here's a simplified version of the Parking Management

System in Python. It:


 Uses a CSV to track slot status.
 Allows vehicle entry and exit.
 Shows availability.

✅ Simple Code
import csv

import os

FILENAME = 'parking.csv'

TOTAL_SLOTS = 5 # You can change the total number of slots

# Initialize parking CSV if not already present

def initialize_parking():

if not os.path.exists(FILENAME):

with open(FILENAME, 'w', newline='') as file:

writer = csv.writer(file)

writer.writerow(['Slot', 'Status'])

for i in range(1, TOTAL_SLOTS + 1):

writer.writerow([i, 'Empty'])
# Load current parking slots

def load_parking():

with open(FILENAME, 'r') as file:

reader = csv.reader(file)

next(reader)

return [[int(row[0]), row[1]] for row in reader]

# Save parking slots to CSV

def save_parking(data):

with open(FILENAME, 'w', newline='') as file:

writer = csv.writer(file)

writer.writerow(['Slot', 'Status'])

writer.writerows(data)

# Display available slots

def show_available(parking):

available = [slot for slot in parking if slot[1] == 'Empty']

print(f"\nAvailable slots: {[slot[0] for slot in available]}")

# Mark slots as occupied

def enter_vehicle(parking, count):


entered = 0

for slot in parking:

if slot[1] == 'Empty' and entered < count:

slot[1] = 'Occupied'

entered += 1

if entered < count:

print(f"Only {entered} vehicles entered. No more free slots.")

else:

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

return parking

# Mark slots as empty

def leave_vehicle(parking, count):

left = 0

for slot in parking:

if slot[1] == 'Occupied' and left < count:

slot[1] = 'Empty'

left += 1

if left < count:

print(f"Only {left} vehicles left. No more occupied slots.")

else:
print(f"{left} vehicle(s) left.")

return parking

# Main loop

def main():

initialize_parking()

while True:

parking = load_parking()

show_available(parking)

try:

in_count = int(input("\nVehicles entering: "))

out_count = int(input("Vehicles leaving: "))

except ValueError:

print("Invalid input. Use numbers only.")

continue

parking = enter_vehicle(parking, in_count)

parking = leave_vehicle(parking, out_count)


save_parking(parking)

cont = input("Continue? (y/n): ").strip().lower()

if cont != 'y':

break

if __name__ == '__main__':

main()

You might also like