gpt4 book ai didi

python - 如何跳过函数?

转载 作者:行者123 更新时间:2023-12-01 03:55:25 25 4
gpt4 key购买 nike

我下面有这段代码。当/如果程序到达 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 (上述函数之后的步骤)

这是向我发送地点的主要代码;

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 中的所有内容吗?至addtototal当我到达if时声明?

最佳答案

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

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

关于python - 如何跳过函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37551263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com