A better choice for your use-case would be an enum. First of all it is reusable, second, if you need to store it in a variable, a check will later read if rating == Rating.Okay, not if rating == 3, so no magic constants.
from enum import Enum class Rating(Enum): Terrible = 1 Bad = 2 Okay = 3 Good = 4 Excellent = 5 def rating(i: int) -> str: try: return Rating(i).name except ValueError: return "Please enter a value between 1-5"
def rating(r):if r is 5:return 'Excellent'elif r is 4:return 'Good'elif r is 3:return 'Okay'elif r is 2:return 'Bad'elif r is 1:return 'Terrible'else:return 'Enter a rating from 1 to 5'- from enum import Enum
- class Rating(Enum):
- Terrible = 1
- Bad = 2
- Okay = 3
- Good = 4
- Excellent = 5
- def rating(i: int) -> str:
- try:
- return Rating(i).name
- except ValueError:
- return "Please enter a value between 1-5"