I am creating a program which will simulate a smart lights system in a home. My code is below with commments which explains what each part does.
import os
# this code retrieves all room names from the text file "rooms.txt"
# if the file is empty, the code will direct the user to another menu to enter room names
def loadSettings():
roomNames = []
file_path = 'rooms.txt'
file = open("rooms.txt", "r")
# checks if the file is empty
if os.path.getsize(file_path) == 0:
print("This appears to be the first time you have used this device, directing you to configuration menu\n")
settings()
else:
reading = True
# reads all the room names from the text file and puts them in a list
while reading == True:
currentLine = file.readline()
if currentLine == "":
reading = False
else:
roomNames.append(currentLine)
print(roomNames)
# when the room names are retrieved from the file, this section of code puts those names into another list, along side with a default status (the lights in that room being 'off')
# the new list should look like this: [['room1', 'off',], ['room2', 'off']]
rooms = [["placeholder"] * 2] * len(roomNames)
for count in range(0, len(roomNames)):
print(roomNames[count])
rooms[count][0] = roomNames[count]
rooms[count][1] = "off"
print(rooms)
# goes back to main menu when the list "rooms" is set-up
mainMenu(rooms)
However, during the execution of the code, the part where the 2D array called 'rooms' is setup, I get an unexpected output.
[['room2\n', 'off],['room2\n', 'off']]
I'm aware of how to remove those '\n's but the final list doesn't have unique rooms. It should look like:
[['room1\n', 'off],['room2\n', 'off']]