Python Forum

Full Version: How to implement the if statement properly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a snip of code listed here. Basically is a database containing motel room numbers. There are 5 rooms in the data base. Some are dirty and some are clean. What I want to do is list all of the rooms into the listbox whether they are dirty or not. I'm having trouble figuring out the correct if statement to use so that I will list all rooms and print '--Dirty' beside the ones that are dirty.


def viewrooms():
    rooms = Toplevel()
    rooms.title('Room List')
    rooms.geometry("800x900+550+50")

    roomlb = Listbox(rooms, height=40, width=30, font="12")
    roomlb.place(x=300, y=20)

    conn = sqlite3.connect('roominventory.db')
    c = conn.cursor()
    c.execute("SELECT * FROM rooms")
    records = c.fetchall()

    for record in records:
        if record[4]=="N":
             room = str(record[0:2])
             status = "--Dirty"

        roomlb.insert(END, room + status)

I got an answer. The following is the correct way:

def viewrooms():
    rooms = Toplevel()
    rooms.title('Room List')
    rooms.geometry("800x900+550+50")

    roomlb = Listbox(rooms, height=40, width=30, font="12")
    roomlb.place(x=300, y=20)

    conn = sqlite3.connect('roominventory.db')
    c = conn.cursor()
    c.execute("SELECT * FROM rooms")
    records = c.fetchall()

    for record in records:
        
        room = str(record[0:2])

        if record[4] == "N":
            room += "--Dirty"

        roomlb.insert(END, room)