2016-06-07 39 views
-1

我有一段代码,它将跳过取决于结果的函数。在一个函数中,我有一个如果找到的语句,这将使结果为真,并通过if下面的代码运行。我也有,如果没有在函数中找到,它会将结果设置为False,以便跳过下面的if语句中的代码。如果结果不止一次使用

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) 

我已经有“如果结果”在此代码,我需要为另一个结果做同样的(跳过或运行基于某些功能,如果结果返回true或false)

这是可能使用变量'result?'

我已经在使用'if result'我能再次做这个吗?'那么,在我定义数量函数的地方是否可以有另一个“if result”呢?

+3

请更改您的问题,以清楚您所问的内容,您的意思是“为其他结果做什么”(什么是“另一结果”和“做同样的事情”),您的意思是“保持通话” – lejlot

+0

在定义之后,您从未更改过变量'result'。所以是的,你可以重复使用它,确保它没有改变。 –

+0

希望这是更清晰的@lejlot –

回答

1

认为你问,如果你能做到这一点:

result = some_function1() 
if result: 
    do() 
    some() 
    things() 

result = some_function2() 
if result: 
    do() 
    some() 
    other() 
    things() 

是。你可以做到这一点。存储在result内的值会随着flow of control的进行而发生变化并且被多次评估。

如果这让你感到困惑,那么可能是你混淆了不同的编程模型imperative and declarative

过度简化,在声明如果你有相同的声明两次(foo = 1234foo = 5678),它可以被视为冲突,因为它不会明确这是预期的定义。在declarative顺序通常没有关系。通过imperative订单和控制流程可以明确foo的值。

这就是说,你应该试试这些东西。做一个小测试,看看会发生什么。 Python是一种用于实验的伟大语言。

相关问题