61.0k views
2 votes
Write a program that keeps track of where cars are located in a parking garage to estimate the time to retrieve a car when the garage is full. This program will demonstrate the following:_________.

How to create a list for use as a stack,
How to enter and change data in a stack.

User Eper
by
7.3k points

1 Answer

1 vote

Answer: Provided in the explanation section

Step-by-step explanation:

Code to use;

# Define the main() function.

def main():

# Declare the required variables.

stack_cars = []

move_time = 1

retrieve_time = 2

# Start the loop to display the menu.

while True:

# Display the choices to the user.

print("\\\t\t\t Car Stack Menu")

print("\\1. Add a car")

print("2. Retrieve a car")

print("3. Show the car stack")

print("4. Exit")

# Prompt the user to enter the input.

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

# Append the car in the stack if the choice is 1.

if choice == '1':

car = input("Enter the car name to add: ")

stack_cars.append(car)

# Remove the car from the stack if the choice is 2.

elif choice == '2':

car = input("Enter the car name to retrieve: ")

# Display the error message if the car is not present.

if car not in stack_cars:

print(car + " is not present in the stack.")

# Otherwise, compute the time to retrieve a car.

else:

# Declare a temp stack to move the cars.

temp = []

x = stack_cars.pop()

count = 0

# Start the loop to pop the cars from the stack

# until the car to be retrieved is found.

while x != car:

count += 1

# Store the car in the temp stack.

temp.append(x)

x = stack_cars.pop()

# Start the loop to move the cars back in the

# original stack.

while len(temp) != 0:

stack_cars.append(temp.pop())

# Compute and display the total time.

total_time = move_time * count + retrieve_time

print("Total time to retrieve", car, "=", total_time)

# Display the stack if the choice is 3.

elif choice == '3':

print("Car stack =", stack_cars)

# Return from the funtion if the choice is 4.

elif choice == '4':

print("Exiting from the program...")

return

# Display the message for invalid input.

else:

print("Error: Invalid choice!")

# Call the main() function.

if __name__ == "__main__":

main()

User Eduard Moraru
by
7.3k points