如何跳过功能?

时间:2016-05-31 16:39:27

标签: python function

我在下面有这段代码。当/如果程序到达if语句:if stocklevel < 1:,我需要直接转到repeat函数。

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 < 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()
                repeat(username)
            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],)
            return stocklevel

如果我将repeat(username)添加到上面代码的底部,我会到达repeat函数,但最后返回quantityFunction(上述函数之后的步骤)< / p>

这是寄给我的主要代码;

while repeatchoice == True:
    code = getproductcode()
    product = checkfile(code)
    stocklevel = checkstocklevel(code)
    quantity = quantityFunction(product)
    checkquantity = isquantityokay(quantity, stocklevel)
    quantity = int(quantity)
    update = updatestocklevel(quantity, stocklevel, code)
    cost = price(product)
    productcost = calculateproductcost(cost, quantity)
    rline = receiptline(product, quantity, productcost)
    addtoreceipt = append(rline)
    addtototal = appendprice(productcost)
    repeatchoice = repeat(username)

当我到达quantityFunction声明时,有没有办法可以跳过从addtototalif的所有内容?

2 个答案:

答案 0 :(得分:1)

你必须稍微改变你的设计。无论如何,您都要告诉代码运行每个步骤,因此在repeat函数结束后,代码仍将执行所有其他步骤。解决此问题的一种方法是将库存级别if语句移出函数,即:

while repeatchoice == True:
    code = getproductcode()
    product = checkfile(code)
    stocklevel = checkstocklevel(code)
    if stocklevel < 1:
        quantity = quantityFunction(product)
        checkquantity = isquantityokay(quantity, stocklevel)
        quantity = int(quantity)
        update = updatestocklevel(quantity, stocklevel, code)
        cost = price(product)
        productcost = calculateproductcost(cost, quantity)
        rline = receiptline(product, quantity, productcost)
        addtoreceipt = append(rline)
        addtototal = appendprice(productcost)
    repeatchoice = repeat(username)

并删除对&#39;重复&#39;在stocklevel函数中。

这很丑陋,添加更多条件会很困难,所以你可能要开始考虑重构这个以使用类/面向对象的编程

答案 1 :(得分:1)

你可以让你的checkstockLevel函数(我猜测第一个代码片段来自)返回一个布尔值(可能除了它已经返回的内容之外),然后使用它。所以像这样:

def checkstocklevel(socklevel):
    #maybe do something (I don't know if there is more code in your function)
    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()
        return False,stocklevel
    #...
    return True,stocklevel

然后在主代码中执行:

while repeatchoice == True:
    code = getproductcode()
    product = checkfile(code)
    result,stocklevel = checkstocklevel(code)
    if result:
        quantity = quantityFunction(product)
        checkquantity = isquantityokay(quantity, stocklevel)
        quantity = int(quantity)
        update = updatestocklevel(quantity, stocklevel, code)
        cost = price(product)
        productcost = calculateproductcost(cost, quantity)
        rline = receiptline(product, quantity, productcost)
        addtoreceipt = append(rline)
        addtototal = appendprice(productcost)
    repeatchoice = repeat(username)

编辑:现在您添加了实际功能,它必须如下所示:

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 < 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()
                    return False,stocklevel
                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],)
                return True,stocklevel