When I index new objects into my list, the newest added object copies to all the previously added objects of the list. I have a small Python program running on a RPI3 taking input from 4 buttons.
full = []
schedule = ['day', 'activity', 'clock']
n = 4
def setDay(x):
while True:
input_state18 = GPIO.input(18)
input_state23 = GPIO.input(23)
input_state24 = GPIO.input(24)
input_state25 = GPIO.input(25)
if input_state23 == False:
print('Monday')
time.sleep(0.2)
x[0] = 'Monday'
break
if input_state18 == False:
print('Tuesday')
time.sleep(0.2)
x[0] = 'Tuesday'
break
if input_state24 == False:
print('Wednesday')
time.sleep(0.2)
x[0] = 'Wednesday'
break
if input_state25 == False:
print('Thursday')
time.sleep(0.2)
x[0] = 'Thursday'
break
def setActivity(x):
#same as setDay
def setClock(x):
#same as setDay
if __name__ == "__main__":
i = 0
while i < n:
setDay(schedule)
setActivity(schedule)
setClock(schedule)
full.insert(i, schedule)
i += 1
#put schedule into first spot in total list
print(full)
When I run the program I get the following output into my terminal:
Monday
Study
19:00
Tuesday
Training
17:00
Monday
Swimming
19:00
Thursday
Training
17:00
[['Thursday', 'Training', '17:00'], ['Thursday', 'Training', '17:00'], ['Thursday', 'Training', '17:00'], ['Thursday', 'Training', '17:00']]
So the problem is located around my while loop in the main function. I tried using list.append as well but I got the same output. How do I change my while loop so the added lists don't replace the previous?