-1

I have a piece of code below which will skip functions dependent on a result. In a function, I have an if found statement, that will make the result true and run through the code below under if. I also have if not found in the function, which will set the result to False, so that the code inside the if statement below is skipped.

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)

I already have 'if result' in this code, and I need to do the same for another result (skip or run certain functions based on if result is returned true or false)

Is this possible using the variable 'result?'

I am already using 'if result' can I do this again?' So, would it be possible to have another 'if result' under where I am defining the quantity function?

  • 3
    Please rephrase your question so it is clear what you are asking about, what do you mean by "do the some for another result" (what is "another result" and "do the same"), what do you mean by "stay called" – lejlot Jun 07 '16 at 21:08
  • You never changed variable `result` after its definition. So yes, you can reuse it being sure it's not changed. – arrakis_sun Jun 07 '16 at 21:08
  • Hopefully that's clearer @lejlot –  Jun 07 '16 at 21:16

1 Answers1

0

I think you're asking if you can do this:

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

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

Yes. You can do that. The value stored within result changes and is evaluated more than once as the flow of control proceeds.

If this is confusing to you, it may be you are confusing the different programming models imperative and declarative.

To over simplify, in declarative if you had the same declaration twice (foo = 1234 and foo = 5678), it could be considered a conflict because it would not be clear which is the intended definition. In declarative order usually doesn't matter. With imperative order and flow of control makes it clear which value foo holds.

That being said, you should try these things. Make a small test and see what happens. Python is a great language for experimenting.

Community
  • 1
  • 1
rrauenza
  • 6,285
  • 4
  • 32
  • 57