lundi 6 décembre 2021

Shorthand for many if statements

I need to do a lot of if statements to determine the way certain data is going.

if mean > 75 and avg <= 5:
  output = 'A'
elif mean > 75 and avg > 5:
  output = 'B'
elif mean > 75 and avg < -5:
  output = 'C'
elif mean > 65 and avg <= 5:
  output = 'D'
elif mean > 65 and avg > 5:
  output = 'E'
etc...

I found another article on Stack Overflow for this problem, but it's for a lot simpler if statements, using only one variable and using each number once. In my example I use single characters, but in the real application I need to use long strings that explain exactly what the numbers mean.

Is there any way I can do what the post I linked does, but using the numbers three times and negating it once like in my example?

Aucun commentaire:

Enregistrer un commentaire