How to filter variables through multiple conditionals












0















I'm writing code that will assign rank to Brazilian Jiu Jitsu Players. The ranking formula is borrowed from chess (ELO Rating method) but it needs some adjustments to properly reflect BJJ. I have three conditional functions which the score must pass through so that it is updated properly:



Elo_Score is already calculated based on original formula and now the score must pass through ONE of the following functions to see if it needs adjustment.



def Weight_Age_Rank_Dif():
"""this functions checks for a certain weight, age AND rank difference
between two players.
(it will kick in if the difference is bigger than 10 kilos, 15 years,
AND 100 rating points; so think of it as a smaller, less experienced
older guy fighting a bigger, more experienced and much younger
opponent)."""

def Weight_Dif():
"""this function checks for a weight difference only (10 or more kilos
between opponents)"""

def Age_Dif():
"""this function checks for an age difference only (10 or more years between
two players AND at least one player over 50; so it would have no effect
between two guys who were 22 and 34)."""

# PRINT RESULTS


The idea behind these functions is to provide a cushion to the underdogs (smaller, less experienced or significantly older guys). If they win, they win more points and if they lose they lose less.



My question is how do we make sure that only ONE of the functions is executed? So if two players met all conditions for the first function than how do we make sure the code skips directly to the # PRINT RESULTS without touching the other two?



Also, each function has multiple conditional statements. For example in the Weight_Age_Rank_Dif() function there are 8 conditional statements. When one of these conditionals is met how can we make the code exit the function, skip the other two functions and go directly to # PRINT RESULTS?



I have tried to make the entire script into one giant conditional statement with a bunch of elif statements but it doesn't work because nested conditionals within elif statements throw an UnboundLocalError and it seems inefficient to put in a global variable each time.










share|improve this question

























  • There's no actual code here. It's very difficult to discuss what code should do without code.

    – Mad Physicist
    Jan 1 at 12:00











  • You want to put your conditions in some kind of structure, like a dictionary or a class. Then test each the conditions in order in a loop, and if one matches execute the adjustment function that they 'guard', and then exit the loop.

    – Martijn Pieters
    Jan 1 at 12:02
















0















I'm writing code that will assign rank to Brazilian Jiu Jitsu Players. The ranking formula is borrowed from chess (ELO Rating method) but it needs some adjustments to properly reflect BJJ. I have three conditional functions which the score must pass through so that it is updated properly:



Elo_Score is already calculated based on original formula and now the score must pass through ONE of the following functions to see if it needs adjustment.



def Weight_Age_Rank_Dif():
"""this functions checks for a certain weight, age AND rank difference
between two players.
(it will kick in if the difference is bigger than 10 kilos, 15 years,
AND 100 rating points; so think of it as a smaller, less experienced
older guy fighting a bigger, more experienced and much younger
opponent)."""

def Weight_Dif():
"""this function checks for a weight difference only (10 or more kilos
between opponents)"""

def Age_Dif():
"""this function checks for an age difference only (10 or more years between
two players AND at least one player over 50; so it would have no effect
between two guys who were 22 and 34)."""

# PRINT RESULTS


The idea behind these functions is to provide a cushion to the underdogs (smaller, less experienced or significantly older guys). If they win, they win more points and if they lose they lose less.



My question is how do we make sure that only ONE of the functions is executed? So if two players met all conditions for the first function than how do we make sure the code skips directly to the # PRINT RESULTS without touching the other two?



Also, each function has multiple conditional statements. For example in the Weight_Age_Rank_Dif() function there are 8 conditional statements. When one of these conditionals is met how can we make the code exit the function, skip the other two functions and go directly to # PRINT RESULTS?



I have tried to make the entire script into one giant conditional statement with a bunch of elif statements but it doesn't work because nested conditionals within elif statements throw an UnboundLocalError and it seems inefficient to put in a global variable each time.










share|improve this question

























  • There's no actual code here. It's very difficult to discuss what code should do without code.

    – Mad Physicist
    Jan 1 at 12:00











  • You want to put your conditions in some kind of structure, like a dictionary or a class. Then test each the conditions in order in a loop, and if one matches execute the adjustment function that they 'guard', and then exit the loop.

    – Martijn Pieters
    Jan 1 at 12:02














0












0








0








I'm writing code that will assign rank to Brazilian Jiu Jitsu Players. The ranking formula is borrowed from chess (ELO Rating method) but it needs some adjustments to properly reflect BJJ. I have three conditional functions which the score must pass through so that it is updated properly:



Elo_Score is already calculated based on original formula and now the score must pass through ONE of the following functions to see if it needs adjustment.



def Weight_Age_Rank_Dif():
"""this functions checks for a certain weight, age AND rank difference
between two players.
(it will kick in if the difference is bigger than 10 kilos, 15 years,
AND 100 rating points; so think of it as a smaller, less experienced
older guy fighting a bigger, more experienced and much younger
opponent)."""

def Weight_Dif():
"""this function checks for a weight difference only (10 or more kilos
between opponents)"""

def Age_Dif():
"""this function checks for an age difference only (10 or more years between
two players AND at least one player over 50; so it would have no effect
between two guys who were 22 and 34)."""

# PRINT RESULTS


The idea behind these functions is to provide a cushion to the underdogs (smaller, less experienced or significantly older guys). If they win, they win more points and if they lose they lose less.



My question is how do we make sure that only ONE of the functions is executed? So if two players met all conditions for the first function than how do we make sure the code skips directly to the # PRINT RESULTS without touching the other two?



Also, each function has multiple conditional statements. For example in the Weight_Age_Rank_Dif() function there are 8 conditional statements. When one of these conditionals is met how can we make the code exit the function, skip the other two functions and go directly to # PRINT RESULTS?



I have tried to make the entire script into one giant conditional statement with a bunch of elif statements but it doesn't work because nested conditionals within elif statements throw an UnboundLocalError and it seems inefficient to put in a global variable each time.










share|improve this question
















I'm writing code that will assign rank to Brazilian Jiu Jitsu Players. The ranking formula is borrowed from chess (ELO Rating method) but it needs some adjustments to properly reflect BJJ. I have three conditional functions which the score must pass through so that it is updated properly:



Elo_Score is already calculated based on original formula and now the score must pass through ONE of the following functions to see if it needs adjustment.



def Weight_Age_Rank_Dif():
"""this functions checks for a certain weight, age AND rank difference
between two players.
(it will kick in if the difference is bigger than 10 kilos, 15 years,
AND 100 rating points; so think of it as a smaller, less experienced
older guy fighting a bigger, more experienced and much younger
opponent)."""

def Weight_Dif():
"""this function checks for a weight difference only (10 or more kilos
between opponents)"""

def Age_Dif():
"""this function checks for an age difference only (10 or more years between
two players AND at least one player over 50; so it would have no effect
between two guys who were 22 and 34)."""

# PRINT RESULTS


The idea behind these functions is to provide a cushion to the underdogs (smaller, less experienced or significantly older guys). If they win, they win more points and if they lose they lose less.



My question is how do we make sure that only ONE of the functions is executed? So if two players met all conditions for the first function than how do we make sure the code skips directly to the # PRINT RESULTS without touching the other two?



Also, each function has multiple conditional statements. For example in the Weight_Age_Rank_Dif() function there are 8 conditional statements. When one of these conditionals is met how can we make the code exit the function, skip the other two functions and go directly to # PRINT RESULTS?



I have tried to make the entire script into one giant conditional statement with a bunch of elif statements but it doesn't work because nested conditionals within elif statements throw an UnboundLocalError and it seems inefficient to put in a global variable each time.







python function if-statement return conditional






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 12:06









Martijn Pieters

717k14025072319




717k14025072319










asked Jan 1 at 11:58









Igor TatarinovIgor Tatarinov

32




32













  • There's no actual code here. It's very difficult to discuss what code should do without code.

    – Mad Physicist
    Jan 1 at 12:00











  • You want to put your conditions in some kind of structure, like a dictionary or a class. Then test each the conditions in order in a loop, and if one matches execute the adjustment function that they 'guard', and then exit the loop.

    – Martijn Pieters
    Jan 1 at 12:02



















  • There's no actual code here. It's very difficult to discuss what code should do without code.

    – Mad Physicist
    Jan 1 at 12:00











  • You want to put your conditions in some kind of structure, like a dictionary or a class. Then test each the conditions in order in a loop, and if one matches execute the adjustment function that they 'guard', and then exit the loop.

    – Martijn Pieters
    Jan 1 at 12:02

















There's no actual code here. It's very difficult to discuss what code should do without code.

– Mad Physicist
Jan 1 at 12:00





There's no actual code here. It's very difficult to discuss what code should do without code.

– Mad Physicist
Jan 1 at 12:00













You want to put your conditions in some kind of structure, like a dictionary or a class. Then test each the conditions in order in a loop, and if one matches execute the adjustment function that they 'guard', and then exit the loop.

– Martijn Pieters
Jan 1 at 12:02





You want to put your conditions in some kind of structure, like a dictionary or a class. Then test each the conditions in order in a loop, and if one matches execute the adjustment function that they 'guard', and then exit the loop.

– Martijn Pieters
Jan 1 at 12:02












1 Answer
1






active

oldest

votes


















0














You want to put your conditions in some kind of structure, like a dictionary or a class. Then test each the conditions in order in a loop, and if one matches execute the adjustment function that they 'guard', and then exit the loop.



For example, you could put the conditions into functions too. Functions in Python are just more objects, you can put a function object in a list just like you can put other things in a list. A list is a good structure here because lists are ordered, so you can order the conditions.



Another option is to simply leave the condition testing to the function, and test if the score changed. If the ELO score changed, skip the other function(s), if not, continue. I'd still put the functions in a list first:



# list of score adjustment functions. Note that they are *not being called*,
# the function objects themselves are referenced here, not what they produce.
adjust_elo_score_functions = [Weight_Age_Rank_Dif, Weight_Dif, Age_Dif]

for adjustment_function in adjust_elo_score_functions:
new_elo_score = adjustment_function(elo_score)
if new_elo_score != elo_score:
# score was adjusted, stop adjusting further
elo_score = new_elo_score
break

# PRINT RESULTS





share|improve this answer
























  • Thank you that is very helpful. One more question, the elo_score is not one variable but rather all possible outcomes of a match between A and B. In Brazilian Jiu Jitsu there can be 10 outcomes (5 for each player: win by submission, win by points, draw, lose by points and lose by submission). Should I put these these in a list as well?

    – Igor Tatarinov
    Jan 5 at 15:39











  • @IgorTatarinov: you'd bundle up those in one object. That can be a dictionary, a tuple, a list or a custom class, whatever works best for your code. That way you can pass around the combined piece of information as one thing. A next step would be to handle adjustments like the ones you are doing here as part of the class that defines the ELO score details.

    – Martijn Pieters
    Jan 5 at 16:28











  • Hey Martijn, what if I just want to calculate the two outcomes that occur as a result of a match? So instead of calculating 10 possible outcomes and selecting only 2 each time (for example: A wins by point, B loses by points) is there a way to calculate only what happens? Thanks again.

    – Igor Tatarinov
    Jan 8 at 6:27











  • I don't know. I don't know the rules of scoring, sorry.

    – Martijn Pieters
    Jan 8 at 10:52











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995235%2fhow-to-filter-variables-through-multiple-conditionals%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You want to put your conditions in some kind of structure, like a dictionary or a class. Then test each the conditions in order in a loop, and if one matches execute the adjustment function that they 'guard', and then exit the loop.



For example, you could put the conditions into functions too. Functions in Python are just more objects, you can put a function object in a list just like you can put other things in a list. A list is a good structure here because lists are ordered, so you can order the conditions.



Another option is to simply leave the condition testing to the function, and test if the score changed. If the ELO score changed, skip the other function(s), if not, continue. I'd still put the functions in a list first:



# list of score adjustment functions. Note that they are *not being called*,
# the function objects themselves are referenced here, not what they produce.
adjust_elo_score_functions = [Weight_Age_Rank_Dif, Weight_Dif, Age_Dif]

for adjustment_function in adjust_elo_score_functions:
new_elo_score = adjustment_function(elo_score)
if new_elo_score != elo_score:
# score was adjusted, stop adjusting further
elo_score = new_elo_score
break

# PRINT RESULTS





share|improve this answer
























  • Thank you that is very helpful. One more question, the elo_score is not one variable but rather all possible outcomes of a match between A and B. In Brazilian Jiu Jitsu there can be 10 outcomes (5 for each player: win by submission, win by points, draw, lose by points and lose by submission). Should I put these these in a list as well?

    – Igor Tatarinov
    Jan 5 at 15:39











  • @IgorTatarinov: you'd bundle up those in one object. That can be a dictionary, a tuple, a list or a custom class, whatever works best for your code. That way you can pass around the combined piece of information as one thing. A next step would be to handle adjustments like the ones you are doing here as part of the class that defines the ELO score details.

    – Martijn Pieters
    Jan 5 at 16:28











  • Hey Martijn, what if I just want to calculate the two outcomes that occur as a result of a match? So instead of calculating 10 possible outcomes and selecting only 2 each time (for example: A wins by point, B loses by points) is there a way to calculate only what happens? Thanks again.

    – Igor Tatarinov
    Jan 8 at 6:27











  • I don't know. I don't know the rules of scoring, sorry.

    – Martijn Pieters
    Jan 8 at 10:52
















0














You want to put your conditions in some kind of structure, like a dictionary or a class. Then test each the conditions in order in a loop, and if one matches execute the adjustment function that they 'guard', and then exit the loop.



For example, you could put the conditions into functions too. Functions in Python are just more objects, you can put a function object in a list just like you can put other things in a list. A list is a good structure here because lists are ordered, so you can order the conditions.



Another option is to simply leave the condition testing to the function, and test if the score changed. If the ELO score changed, skip the other function(s), if not, continue. I'd still put the functions in a list first:



# list of score adjustment functions. Note that they are *not being called*,
# the function objects themselves are referenced here, not what they produce.
adjust_elo_score_functions = [Weight_Age_Rank_Dif, Weight_Dif, Age_Dif]

for adjustment_function in adjust_elo_score_functions:
new_elo_score = adjustment_function(elo_score)
if new_elo_score != elo_score:
# score was adjusted, stop adjusting further
elo_score = new_elo_score
break

# PRINT RESULTS





share|improve this answer
























  • Thank you that is very helpful. One more question, the elo_score is not one variable but rather all possible outcomes of a match between A and B. In Brazilian Jiu Jitsu there can be 10 outcomes (5 for each player: win by submission, win by points, draw, lose by points and lose by submission). Should I put these these in a list as well?

    – Igor Tatarinov
    Jan 5 at 15:39











  • @IgorTatarinov: you'd bundle up those in one object. That can be a dictionary, a tuple, a list or a custom class, whatever works best for your code. That way you can pass around the combined piece of information as one thing. A next step would be to handle adjustments like the ones you are doing here as part of the class that defines the ELO score details.

    – Martijn Pieters
    Jan 5 at 16:28











  • Hey Martijn, what if I just want to calculate the two outcomes that occur as a result of a match? So instead of calculating 10 possible outcomes and selecting only 2 each time (for example: A wins by point, B loses by points) is there a way to calculate only what happens? Thanks again.

    – Igor Tatarinov
    Jan 8 at 6:27











  • I don't know. I don't know the rules of scoring, sorry.

    – Martijn Pieters
    Jan 8 at 10:52














0












0








0







You want to put your conditions in some kind of structure, like a dictionary or a class. Then test each the conditions in order in a loop, and if one matches execute the adjustment function that they 'guard', and then exit the loop.



For example, you could put the conditions into functions too. Functions in Python are just more objects, you can put a function object in a list just like you can put other things in a list. A list is a good structure here because lists are ordered, so you can order the conditions.



Another option is to simply leave the condition testing to the function, and test if the score changed. If the ELO score changed, skip the other function(s), if not, continue. I'd still put the functions in a list first:



# list of score adjustment functions. Note that they are *not being called*,
# the function objects themselves are referenced here, not what they produce.
adjust_elo_score_functions = [Weight_Age_Rank_Dif, Weight_Dif, Age_Dif]

for adjustment_function in adjust_elo_score_functions:
new_elo_score = adjustment_function(elo_score)
if new_elo_score != elo_score:
# score was adjusted, stop adjusting further
elo_score = new_elo_score
break

# PRINT RESULTS





share|improve this answer













You want to put your conditions in some kind of structure, like a dictionary or a class. Then test each the conditions in order in a loop, and if one matches execute the adjustment function that they 'guard', and then exit the loop.



For example, you could put the conditions into functions too. Functions in Python are just more objects, you can put a function object in a list just like you can put other things in a list. A list is a good structure here because lists are ordered, so you can order the conditions.



Another option is to simply leave the condition testing to the function, and test if the score changed. If the ELO score changed, skip the other function(s), if not, continue. I'd still put the functions in a list first:



# list of score adjustment functions. Note that they are *not being called*,
# the function objects themselves are referenced here, not what they produce.
adjust_elo_score_functions = [Weight_Age_Rank_Dif, Weight_Dif, Age_Dif]

for adjustment_function in adjust_elo_score_functions:
new_elo_score = adjustment_function(elo_score)
if new_elo_score != elo_score:
# score was adjusted, stop adjusting further
elo_score = new_elo_score
break

# PRINT RESULTS






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 12:10









Martijn PietersMartijn Pieters

717k14025072319




717k14025072319













  • Thank you that is very helpful. One more question, the elo_score is not one variable but rather all possible outcomes of a match between A and B. In Brazilian Jiu Jitsu there can be 10 outcomes (5 for each player: win by submission, win by points, draw, lose by points and lose by submission). Should I put these these in a list as well?

    – Igor Tatarinov
    Jan 5 at 15:39











  • @IgorTatarinov: you'd bundle up those in one object. That can be a dictionary, a tuple, a list or a custom class, whatever works best for your code. That way you can pass around the combined piece of information as one thing. A next step would be to handle adjustments like the ones you are doing here as part of the class that defines the ELO score details.

    – Martijn Pieters
    Jan 5 at 16:28











  • Hey Martijn, what if I just want to calculate the two outcomes that occur as a result of a match? So instead of calculating 10 possible outcomes and selecting only 2 each time (for example: A wins by point, B loses by points) is there a way to calculate only what happens? Thanks again.

    – Igor Tatarinov
    Jan 8 at 6:27











  • I don't know. I don't know the rules of scoring, sorry.

    – Martijn Pieters
    Jan 8 at 10:52



















  • Thank you that is very helpful. One more question, the elo_score is not one variable but rather all possible outcomes of a match between A and B. In Brazilian Jiu Jitsu there can be 10 outcomes (5 for each player: win by submission, win by points, draw, lose by points and lose by submission). Should I put these these in a list as well?

    – Igor Tatarinov
    Jan 5 at 15:39











  • @IgorTatarinov: you'd bundle up those in one object. That can be a dictionary, a tuple, a list or a custom class, whatever works best for your code. That way you can pass around the combined piece of information as one thing. A next step would be to handle adjustments like the ones you are doing here as part of the class that defines the ELO score details.

    – Martijn Pieters
    Jan 5 at 16:28











  • Hey Martijn, what if I just want to calculate the two outcomes that occur as a result of a match? So instead of calculating 10 possible outcomes and selecting only 2 each time (for example: A wins by point, B loses by points) is there a way to calculate only what happens? Thanks again.

    – Igor Tatarinov
    Jan 8 at 6:27











  • I don't know. I don't know the rules of scoring, sorry.

    – Martijn Pieters
    Jan 8 at 10:52

















Thank you that is very helpful. One more question, the elo_score is not one variable but rather all possible outcomes of a match between A and B. In Brazilian Jiu Jitsu there can be 10 outcomes (5 for each player: win by submission, win by points, draw, lose by points and lose by submission). Should I put these these in a list as well?

– Igor Tatarinov
Jan 5 at 15:39





Thank you that is very helpful. One more question, the elo_score is not one variable but rather all possible outcomes of a match between A and B. In Brazilian Jiu Jitsu there can be 10 outcomes (5 for each player: win by submission, win by points, draw, lose by points and lose by submission). Should I put these these in a list as well?

– Igor Tatarinov
Jan 5 at 15:39













@IgorTatarinov: you'd bundle up those in one object. That can be a dictionary, a tuple, a list or a custom class, whatever works best for your code. That way you can pass around the combined piece of information as one thing. A next step would be to handle adjustments like the ones you are doing here as part of the class that defines the ELO score details.

– Martijn Pieters
Jan 5 at 16:28





@IgorTatarinov: you'd bundle up those in one object. That can be a dictionary, a tuple, a list or a custom class, whatever works best for your code. That way you can pass around the combined piece of information as one thing. A next step would be to handle adjustments like the ones you are doing here as part of the class that defines the ELO score details.

– Martijn Pieters
Jan 5 at 16:28













Hey Martijn, what if I just want to calculate the two outcomes that occur as a result of a match? So instead of calculating 10 possible outcomes and selecting only 2 each time (for example: A wins by point, B loses by points) is there a way to calculate only what happens? Thanks again.

– Igor Tatarinov
Jan 8 at 6:27





Hey Martijn, what if I just want to calculate the two outcomes that occur as a result of a match? So instead of calculating 10 possible outcomes and selecting only 2 each time (for example: A wins by point, B loses by points) is there a way to calculate only what happens? Thanks again.

– Igor Tatarinov
Jan 8 at 6:27













I don't know. I don't know the rules of scoring, sorry.

– Martijn Pieters
Jan 8 at 10:52





I don't know. I don't know the rules of scoring, sorry.

– Martijn Pieters
Jan 8 at 10:52




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995235%2fhow-to-filter-variables-through-multiple-conditionals%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]