0

What's wrong with my code?

Complete the program to inform the user about available spaces in the parking lot. # Take the number of available spaces as an input spaces = int(input()) # Display message if spaces are available spaces = 20 if spaces >= 0: print("Available spaces") # Display a different message if spaces are not available else: print("Sorry, the parking lot is full")

8th Apr 2025, 12:52 AM
Justin Rei Cargullo
Justin Rei Cargullo - avatar
2 Antworten
+ 5
Bronyth are you trying to input() spaces or hardcode spaces ? read the instructions back Secondly how can a car / vehicle park in 0 spaces ... A smart parking lot displays different messages to the visitor based on the number of available spaces.   Task Complete the program to inform the user about available spaces in the parking lot
8th Apr 2025, 1:14 AM
BroFar
BroFar - avatar
+ 1
The original code has a few issues. First, the condition spaces >= 0 is incorrect because it is always true, as the number of parking spaces is always a non-negative number (0 or greater). This means that the message "Available spaces" will also be displayed when there are no free spaces left, i.e., when spaces == 0. To fix this, the condition should be spaces > 0, so that the message is only shown if at least one parking space is still available. If no spaces are left (i.e., spaces == 0), the message "Sorry, the parking lot is full" should be displayed instead. Another issue with the code is that the value for spaces was hardcoded, meaning it was always 20. This doesn't allow the user to enter their own number. Instead, the user input should be requested using spaces = int(input()), so the user can enter the number of available parking spaces themselves.
9th Apr 2025, 3:47 PM
Max Wolfrum
Max Wolfrum - avatar