0

I want to know if I can use a loop in python to make it into single block of code that would fetch me best_result1_conf, best_result2_conf and best_result3_conf.

if best_score1 == '1.0':    
    best_result1_conf='High'
elif best_score1 > '0.85' and best_score1 < '1.0':
    best_result1_conf='Medium'
else: best_result1_conf='Low'

if best_score2 == '1.0':    
    best_result2_conf='High'
elif best_score2 > '0.85' and best_score2 < '1.0':
    best_result2_conf='Medium'
else: best_result2_conf='Low'

if best_score3 == '1.0':    
    best_result3_conf='High'
elif best_score3 > '0.85' and best_score3 < '1.0':
    best_result3_conf='Medium'
else: best_result3_conf='Low'
3
  • 3
    You could make a function that takes a score and returns a result, that would reduce duplication. Also, store both in lists, rather than separate variables, it's much more scalable: results = [s_to_r(score) for score in scores] Commented Apr 11, 2014 at 20:10
  • You should use list to store your multiple variables. Commented Apr 11, 2014 at 20:11
  • you should use a function here. Commented Apr 11, 2014 at 20:12

3 Answers 3

2

As a function:

def s_to_r(s):
    if 0.85 < s < 1.0:
        return "Medium"
    elif s == 1.0:
        return "High"
    else:
        return "Low"

results = [s_to_r(score) for score in [best_score1, best_score2, best_score3] ]

Though usually this is when I'd like to introduce OOP. I'm assuming these scores BELONG to someone, so maybe:

class Competitor(object):
    def __init__(self, name):
        self.name = name
        self.scores = list()
    def addScore(self,score):
        self.scores.append(score)
    def _getScoreValue(self,index):
        score = self.scores[index]
        if score <= 0.85:
            return "Low"
        elif 0.85 < score < 1.0:
            return "Medium"
        else:
            return "High"
    def getScore(self,index):
        return {"score":self.scores[index],"value":_getScoreValue(index)}

This will let you do things like:

competitors = [Competitor("Adam"),Competitor("Steven"),Competitor("George"),
               Competitor("Charlie"),Competitor("Bob"),Competitor("Sally")]
# generating test data
for competitor in competitors:
    for _ in range(5):
        competitor.addScore(round(random.random(),2))
# generating test data

for competitor in competitors:
    for i,score in enumerate(competitor.scores):
        if i==0: name = competitor.name
        else:    name = ""
        print("{name:20}{scoredict[score]:<7}{scoredict[value]}".format(name=name,
                                                 scoredict=competitor.getScore(i)))
Sign up to request clarification or add additional context in comments.

6 Comments

else: return "Low" - no, no, no! if score == 2 its not low!
@akaRem I'd have implemented sanity checks for the score, but since OP's code in his question implies that the only possible values are between 0 and 1, it's unnecessary. His own code (which presumably is working as intended) lists else: "Low"
I dont agree. I think that if-elif shall look like if < 0.85 / if < 1 / if == 1 / else raise .. - this is more strict.
optionally add check for <0
Every data in [0,1.0] has values out of bands too often %)
|
0

You can use lists.

best_scores = [1.0, 0.9, 0.7]
results = []

for score in best_scores:
    if score == 1.0:
        results.append('High')
    elif score > 0.85 and score < 1.0:
        results.append('Medium')
    else:
        results.append('Low')

2 Comments

elif score > 0.85 and score < 1.0 - I'm sure, that at this elif score is definitely <1 (if score could be >1, your logic does not work at all), so, you may just write elif score > 0.85:
True enough. I was emulating the original code to make the answer easier to understand for the one who asked the question.
0

Using a function:

def find_result(result):
    if result > 1.0:
        print(“Cannot calculate”)
    elif result == 1.0:    
        word_result = ‘High’
    elif result > 0.85:
        word_result = ‘Medium’
    else: 
        word_result = ‘Low’
    return word_result
best_result1_conf = find_result(best_score1)
best_result2_conf = find_result(best_score2)
best_result3_conf = find_result(best_score3)

Hope this helps!

1 Comment

what if result == 2? It definitely not low!

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.