'Modify Array Based on User Input
For my assignment, we are currently coding a hotel booking system. For each room booked I want to make it so that a room that was previously available when selected is eliminated from the array. This is my array: availableRooms[38, 48, 64, 89, 110, 134, 224, 306, 402]
So pretty much it involves sequential file reading and writing and my mate who is like a Python Kami told me I should create a separate txt file as follows with 0 meaning available and 1 meaning unavailable:
0, 0, 0, 0, 0, 0, 0, 0, 0 when split = [0, 0, 0, 0, 0, 0, 0, 0, 0]
So if I were to pick room 38, the first zero in this array would turn into a 1 and it would remove said room from the availableNumbers array...I think.
If anyone is able to provide me with assistance or code in how to do so, your support would be greatly appreciated as the deadline for the assignment is nearing closer and closer.
Solution 1:[1]
allRooms = [38, 48, 64, 89, 110, 134, 224, 306, 402]
availableRooms = [38, 48, 64, 89, 110, 134, 224, 306, 402] # Your array
selected = input("What room would you like? These are the available rooms: ", availableRooms) # Asks user to input room
for 0 to len(availableRooms): # For all values of the array
arrayPosition = 0
if selected == availableRooms[arrayPosition]: # If "selected" is the position
del availableRooms[arrayPosition] # delete it from "availableRooms"
else:
arrayPosition = arrayPosition + 1
if len(availableRooms) = len(allRooms): # If unsuccessful input
print("You have not selected a valid room.")
break
else:
print("You have booked Room ", selected ". Enjoy your stay!")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | taylor.2317 |
