0
def checkQuota(candidateX_Votes, candidateX_Won):
    quota = int((validVotesCast / (seatsAvaliable + 1)) + 1)

    if candidateX_Votes < quota:
        candidateX_Won = False
        return candidateX_Won

    elif candidateX_Votes >= quota:
        candidateX_Won = True
        return candidateX_Won

The goal is to have the value of either true or false set as the variable that is put into the function, for example, if I do checkQuota(candidateA_Votes, candidateA_Won) I should be able to use candidateA_Won later on where it is either assigned the value of true or false.

1
  • 1
    Why are you passing candidateX_Won into the function if you are also using it as the return value? Commented Oct 1, 2018 at 22:55

2 Answers 2

1

Since you don't actually use candidateA_Won as a parameter, I would recommend you refactor to instead just return the value you want and store that in an external variable, as follows:

def checkQuota(candidateX_Votes): # Get rid of the parameter
    quota = int((validVotesCast / (seatsAvaliable + 1)) + 1)

    if candidateX_Votes < quota:
        candidateX_Won = False
        return candidateX_Won

    elif candidateX_Votes >= quota:
        candidateX_Won = True
        return candidateX_Won

Then, you can use the function as follows:

candidateX_Won = checkQuota(candidateX_Votes) # Without having to pass in a variable

In fact you can even simplify the function above to:

def checkQuota(candidateX_Votes):
    return not (candidateX_Votes < int((validVotesCast / (seatsAvaliable + 1)) + 1))
Sign up to request clarification or add additional context in comments.

Comments

0

your variable candidateX_Won is local in the function scope. When you assign a True or False you change a local variable instead of the original one. To get the result you want, you can use this example:

def checkQuota(candidate_x_votes):
    # here you calculate/get validVotesCast and seatsAvaliable
    quota = int((validVotesCast / (seatsAvaliable + 1)) + 1)
    return not (candidate_x_votes < quota)  # this will return True or False

# and after that you can use your function
candidateX_Votes = 12 # for example 
candidateX_Won = checkQuota(candidateX_Votes)

Comments

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.