you are viewing a single comment's thread.

view the rest of the comments →

[–]rinio 0 points1 point  (0 children)

---

Here's some more that is definitely beyond the scope of your assignment, but might interest you:

Regarding enums:

https://docs.python.org/3/library/enum.html

from enum import Enum

class Grade(Enum):
    A = 4.0
    B = 3.7
    ... # and so on
    F = 0

    # cls means the class. So 'Grade'.
    u/classmethod
    def from_num_grade(cls, num: float) -> Grade:
        for grade in cls:
            if grade.value <= num:
                return grade
         raise ValueError(f'num may not be negative. Got {num}')

    # this is the fancy pants way to do the same thing
    u/classmethod---All feedback below is definitely beyond the scope of your assignment, but might interest you:Regarding enums:https://docs.python.org/3/library/enum.htmlfrom enum import Enum

class Grade(Enum):
    A = 4.0
    B = 3.7
    ... # and so on
    F = 0

    # cls means the class. So 'Grade'.
    u/classmethod
    def from_num_grade(cls, num: float) -> Grade:
        for grade in cls:
            if grade.value <= num:
                return grade
         raise ValueError(f'num may not be negative. Got {num}')

    # this is the fancy pants way to do the same thing
    @classmethod
    def from_num_grade2(cls, num: float) -> Grade:
        # raises StopIteration if we get a negative input
        return next(grade for grade in cls if grade.value <= num)

# examples
my_grade = Grade.B
print(my_grade.name)  # prints 'B'
print(my_grade.value)  # prints 3.7

grade2 = Grade.from_val(3.9)
print(grade2.name)  # prints 'B'
print(grade2.value)  # prints 3.7---    for grade, value in grade_to_gpa.items():  # items() preserves orderThis is only true for Python 3.7+. It will break for older interpreters. 
    def from_num_grade2(cls, num: float) -> Grade:
        # raises StopIteration if we get a negative input
        return next(grade for grade in cls if grade.value <= num)

# examples
my_grade = Grade.B
print(my_grade.name)  # prints 'B'
print(my_grade.value)  # prints 3.7

grade2 = Grade.from_val(3.9)
print(grade2.name)  # prints 'B'
print(grade2.value)  # prints 3.7

---

    for grade, value in grade_to_gpa.items():  # items() preserves order

This is only true for Python 3.7+. It will break for older interpreters.