0

I have function AB which has 1 parameter, and AB has a bunch of loops and I have a counter which keeps track of how much it loops. It (must) returns an either True or False answer.

I have a second function called AC which calls upon 2 instances of AB with different things for the parameters and compares them like so

if (AB(option1) == True and AB(option2) == False):
     result = "Option1 Wins"
elif (AB(option1) == False and AB(option2) == True):
     result = "Option2 Wins"
if (AB(option1) == True and AB(option2) == True):
     result = ??

however if both cases are 'True', I need to know which case reached 'True' first, so this means I would need to know how many times it loops (i need to know the value of the counter variable)

How can I access the variable?

I was thinking of making a helper function to access it but I'm not sure if that's possible/how to do that

4
  • Must AB only return True or False? If we can return more than that it helps. Commented Feb 11, 2017 at 23:59
  • Can we modify the parameter, e.g. option1? Commented Feb 12, 2017 at 0:01
  • AB must return a Boolean and what exactly do you mean by modify the parameter? I'm, 99% sure we're not allowed to unless you're thinking of something different Commented Feb 12, 2017 at 0:03
  • Why must AB return a boolean? Integers can be interpreted as boolean, using the C convention that 0 is false and everything else is true. Why not return zero to fail, and return your loop index to pass? Commented Feb 12, 2017 at 0:06

4 Answers 4

2

You can return more than one value from a function in python:

def AB(param):
   return True, 1

val = AB(1) # it will be (True,1), so it's a set
val, val1 = AB(2) # val = True, val1 = 1

So in your code you should check AB(param)[0] to check True, if you want to keep only one return value (you should get them in different variables).

But your code is wrong, since you are calling AB() each time you are checking the output, which will execute all the loops and, eventually, could bring an unexpected response for the same input. You should get the output from AB() for each param before, and then check them in your if block.

Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, naturally I wanted to do that as well but each function is being checked seperately and it must only return a boolean
@HelloMellow check my edited response, I clicked on send too soon. Your code has some problems because you are executing the functions all the time.
That's a good idea, I'll get the output before and then check it in the if block after, but for the AB function, I can only strictly return a boolean. So it has to be return True and can't be return True, 1
@HelloMellow I can see another response mentioning globals, which I'm not friend of using. If you can add a parameter, then pass the counter of loops as a second parameter, and change it in the function. That way, you'll return True or False, and the counter itself.
That's smart too, but generally were not allowed to change the parameters or return things other than what's expected, and I checked the file, and our prof has written "I discourage use of optional parameters. You can always get around it by writing a separate function (like a helper function)." I'm 100% sure he'll take off marks for not following "his standards" if I do end up adding parameters :\
0

Zero evaluates to False and non-zero evaluates to True:

If AB fails it returns 0 otherwise it returns the amount of loops:

def AB(param):
    if failed:
        return 0
    else:
        return counter

Usage:

if AB(option1) and not AB(option2):
    result = "Option1 Wins"
elif not AB(option1) and AB(option2):
    result = "Option2 Wins"
elif AB(option1) < AB(option2):
    result = "Option1 Wins"
else:
    result = "Option2 Wins"

3 Comments

Ah, this makes sense, tho one question, if I were to run the AB function alone, wouldn't it return an integer? Because the AB function will be tested on it's own and it should return a Boolean.
If it must return only a value of type bool i.e. only True or False I don't know how to answer the question without using a global as the counter.
If you test AB in a boolean expression, rather than comparing the return values against bools, it will work fine.
0

OOP solution: Create a class with this methods as member function and the counter variable as a member variable. Now you can check that value from anywhere for any object of that class.

Also in your code, no need to invoke AB multiple times.

o1 = AB(option1)
o2 = AB(option2)
if o1 and not o2:
     result = "Option1 Wins"
elif not o1 and o2):
     result = "Option2 Wins"
elif o1 and o2:
     result = ??

An object oriented solution might look like following (code not tested)

class solver(object, option):
    reached = 0
    option = option
    status = False

    def AB(self):
        # implementation. use self.option and update self.reached and self.status

opt1 = solver(option1)
opt2 = solver(option2)
o1 = opt1.AB()
o2 = opt2.AB()

if o1.status and not o2.status:
     result = "Option1 Wins"
elif not o1.status and o2.status):
     result = "Option2 Wins"
elif o1 and o2:
     if o1.reached < o2.reached:
         # o1 reached first

Comments

-1

You can use global variables to store values of the counters. See http://www.diveintopython.net/html_processing/locals_and_globals.html for more details.

2 Comments

wow I can't believe I didn't think of that as an answer, but sadly our prof is strictly against global variables. Maybe that's why I didn't think about it haha
How would you know which counter was which?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.