Python Forum
How to implement the if statement properly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to implement the if statement properly
#1
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to implement the if then else statement properly. scratchmyhead 5 2,318 May-16-2020, 07:16 AM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020