user3450198
user3450198

Reputation: 39

Using for/while loop in python to generate variables and do computation

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'

Upvotes: 0

Views: 117

Answers (3)

Quintec
Quintec

Reputation: 1114

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!

Upvotes: 0

Adam Smith
Adam Smith

Reputation: 54183

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)))

Upvotes: 2

Platinum Azure
Platinum Azure

Reputation: 46183

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')

Upvotes: 0

Related Questions