簡體   English   中英

第二次運行程序時,我不知道如何將列表設置為移動到下一個元素

[英]I do not know how to set the list to move to the next element when run the program the second time

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)

對於此程序,我想將列表self.not_served_deluxe的第一個元素設置為 101,然后如果第二次運行該程序,則將第一個元素移動到 102。 但是我不知道應該使用什么方法才能讓第二個元素在下次運行程序時成為第一個元素。 如果可能,有人可以幫忙解決嗎?

第一個解釋:

您可以創建一個新變量,您將其稱為計數器,然后在每次經過時進行計數,然后執行 if 語句

偽代碼:if variable =1-> execution1 else -> execution 2

第二種解釋:

因此,如果我正確理解您,您希望在程序運行 1 次時執行不同的程序。 那是對的嗎? 如果是,則創建一個變量來計算您瀏覽代碼的頻率,然后從該變量創建一個 if 語句,其中包含必要的語句。 你明白我的意思嗎? 或者我應該在哪個地方更詳細地解釋它

如果你想在腳本運行之間存儲一些東西,你應該把它寫到一個文件中。 有很多選項,比如picklecsv ,或者只是打開一個文件並寫入它。

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

下一次運行

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

# ... Do stuff here

with open("./myfile.txt", "w") as file:
    file.write(room + 1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM