samedi 23 mai 2020

Is there a way of reducing the quantity nested if/elif statements for this problem?

I am trying to find the maximum of two numbers(totMax and totMin) each has a corresponding lists (totMaxList and totMinList). I need to store the corresponding list to that number it in the variable 'highest', assuming both numbers are both bellow 20. If only one of the numbers meets the condition, then the corresponding list to that number will be stored. The number stored in totMax with always be higher that the number store in totMin. Is there a more concise way of doing this?

if totMax > 20 and totMin > 20: 
      raise ValueError(f"Both out of range")
    elif totMax <= 20:
      highest = totMaxList
    elif totMin <= 20:
      highest = totMinList
    return highest 

Aucun commentaire:

Enregistrer un commentaire