Q:

Printing wrong if statement

I have this data in a CSV
34512340,0
12395675,2
56756777,1

My code below is checking what the stock level of each product is. The product is the eight digit number in the csv, and the number after the comma is the amount of available stock.
If the stock level is above 0, all works fine.
However, when the stock level gets to 0, instead out printing the out of stock message, the program prints the new order message. Can anybody work out why this is happening?
def checkstocklevel(code):
    with open('stockcontrol.csv',newline='') as f:
        for line in f:
            if code in line:
                data = line.split(",")
                stocklevel = int(data[1])
                if stocklevel <= 5:
                    print("New Order Required - Remaining Stock:",data[1],)
                elif stocklevel <= 10:
                    print("Low Stock - Remaining Stock:",data[1],)
                elif stocklevel < 1:
                    print("Sorry, this product is out of stock")
                    f = open("receipts","a")
                    f.write(code)
                    f.write(" Product Out Of Stock\n")
                    f.close()
                else:
                    print("Normal Stock -",data[1],)
                return stocklevel

Thanks

A:

A value of 0 already matches your first condition stocklevel <= 5 so your later condition < 1 is never reached.
Reorder your conditions. Start with the most strict condition, then relax them.
            if stocklevel < 1:
                print("Sorry, this product is out of stock")
                f = open("receipts","a")
                f.write(code)
                f.write(" Product Out Of Stock\n")
                f.close()
            elif stocklevel <= 5:
                print("New Order Required - Remaining Stock:",data[1],)
            elif stocklevel <= 10:
                print("Low Stock - Remaining Stock:",data[1],)
            else:
                print("Normal Stock -",data[1],)

