0
def get_room(self):
        if self.var2.get() == 'Deluxe Room':
            self.not_served_deluxe = [x for x in range(101,200)]
            self.served_deluxe = []

            self.take_room = self.not_served_deluxe[0]
            self.not_served_deluxe.remove(self.take_room)
            self.served_deluxe.append(self.take_room)

            self.room_win = tk.Tk()
            self.room_win.geometry('400x600')

            self.room_lb = tk.Label(self.room_win,text='Room No.',font=('Helvetica',20))
            self.room_lb.grid(row=0,column=0,padx=20,pady=10)

For this program, I would like to set the first element of list self.not_served_deluxe into 101 then if run the program second time move to 102 for the first element. However I do not know what method should I use in order to let the second element to be the first one when run the program next time. If possible can anyone help to solve?

Dom807
  • 27
  • 7
  • Would you want to start on 103 the third time? You're going to need to track how many times the function was called. Something here might help: https://stackoverflow.com/questions/21716940/is-there-a-way-to-track-the-number-of-times-a-function-is-called – dbramwell Nov 20 '20 at 08:36

2 Answers2

0

first explanation:

you could create a new variable, which you call a counter and then count up every time it has gone through and then just make an if statement

pseudocode: if variable =1-> execution1 else -> execution two

second explanation:

so if i understand you correctly you want to do a different execution of the program when the program is run through 1 time. is that correct? If yes, then create a variable that counts how often you go through the code and then create an if statement from this variable, which contains the necessary statement. Do you understand what I mean? Or in which place should I explain it in more detail

  • Sorry I do not understand what it is. Can you explain? – Dom807 Nov 20 '20 at 08:35
  • so if i understand you correctly you want to do a different execution of the program when the program is run through 1 time. is that correct? If yes, then create a variable that counts how often you go through the code and then create an if statement from this variable, which contains the necessary statement. Do you understand what I mean? Or in which place should I explain it in more detail – Paul_Hofmann Nov 20 '20 at 08:37
  • @Paul_Hofmann Maybe update the answer with the explanation – Delrius Euphoria Nov 20 '20 at 09:15
  • Any suggested code so that I could understand how to move the list to the next one each run? – Dom807 Nov 20 '20 at 09:27
  • I still didn't get the meaning – Dom807 Nov 20 '20 at 10:33
0

If you want to store something between runs of the script you should write it to a file. There are many options like pickle, csv, or just opening a file and writing to it.

with open("./myfile.txt", "w") as file:
    file.write(101)

Next run

with open("./myfile.txt", "r") as file:
    room = (file.read())

# ... Do stuff here

with open("./myfile.txt", "w") as file:
    file.write(room + 1)
Gijs Wobben
  • 1,974
  • 1
  • 10
  • 13
  • Is it possible to run the second time for 102 then third time 103 then so on...? – Dom807 Nov 20 '20 at 08:37
  • Sure! Just read the value from file, increment, and write back to file. – Gijs Wobben Nov 20 '20 at 08:39
  • May I ask how about if I want to store in csv to put the data on the rows one by one? – Dom807 Nov 20 '20 at 08:42
  • Yes that would work too. It doesn't seem very related to your question so I won't update the answer, but there are a ton of great tutorials out there for doing this with CSV: https://realpython.com/python-csv/ – Gijs Wobben Nov 20 '20 at 08:43
  • So do I need to change each time before each run? – Dom807 Nov 20 '20 at 08:45
  • You load the last number from file (101 after the first run), then do stuff (I don't know your program), increment the number (so now its 102) and save that to file. And then repeat – Gijs Wobben Nov 20 '20 at 08:52