As I ended up with many ugly if-elif's when comparing temperature to determine an adjective for the temperature, I thought that I can look for the temperature value in a list in a dictionary, where its key will be the corresponding adjective for the temperature:
def deternmine_temp(temp):
temps = {
'FREEZING' : [i for i in range(-20, 0)],
'very cold' : [i for i in range(0, 6)],
'cold' : [i for i in range(6, 11)],
'chilly' : [i for i in range(11, 16)],
'cool' : [i for i in range(16, 21)],
'warm' : [i for i in range(21, 26)],
'hot' : [i for i in range(26, 31)],
'very hot' : [i for i in range(31, 36)],
'dangerously hot' : [i for i in range(36, 40)],
'extremely dangerously hot': [i for i in range(41, 46)]
}
temp = int(temp.replace('°', '').strip())
for word, values in temps.items():
if temp in values:
return word
This is a lot better than 7+ if-elif's, but I don't think it is very efficient, especially if temps had a lot more data (for example if I had a narrower range of values that correspond to an adjective).
What would be some ways to make this more efficient? Maybe some functions in the dictionary? Above is really the best I can think of.