in the attached code I add a loop which selects a bottle from a list of bottles. This selection should be saved in a file and checked the next time you run it. If the new selection should be the same as the one saved in the file, then the loop should run until the contents are not the same.
The selection from the next run should then be saved in the file.
The program runs so far, a selection is made, the contents of the file are read and displayed. Finally, the selection is also saved in the file and replaced by the new one the next time it is run.
However, I have a problem with the part when the selected bottle is the same as the previous one.
I'm still at the beginning of programming in Python, maybe you can give me a tip or tell me what I'm doing wrong.
Thank you in advance!
bottle.py is an empty file the beginning, just for saving der choosen bottle of the last run.
Code: Select all
import random
import time
##Select which bottle to take
selectbottle = random.choice(["1","2","3","4"])
bottle = open('/home/pi/Scripts/bottle.py','r')
formerbottle = bottle.readlines()
bottle.close()
print(selectbottle)
print(formerbottle)
time.sleep(0.5)
while True:
if selectbottle == formerbottle:
print("equal")
if not selectbottle == formerbottle:
f = open('/home/pi/Scripts/bottle.py','w')
f.write(selectbottle)
f.close()
break
break
time.sleep(0.5)