Skip to main content
Became Hot Network Question
Spelling and grammar
Source Link
Toby Speight
  • 81.8k
  • 14
  • 101
  • 309

Simple Week Program in Pythonday-of-week program

iI was doing the following exercise:

I tried to do it in a object-oriented way and using Python. Can someone please verify if my code is within the Python and object-oriented "standards"? Because i'mI'm not sure ifwhether it is, and iI need a review.

Simple Week Program in Python

i was doing the following exercise:

I tried to do it in a object-oriented way and using Python. Can someone please verify if my code is within the Python and object-oriented "standards"? Because i'm not sure if it is and i need a review.

Simple day-of-week program

I was doing the following exercise:

I tried to do it in a object-oriented way and using Python. Can someone please verify if my code is within the Python and object-oriented "standards"? Because I'm not sure whether it is, and I need a review.

Tweeted twitter.com/StackCodeReview/status/1397432309603880961
Source Link

Simple Week Program in Python

i was doing the following exercise:

Create an algorithm which asks the user a number from 1 to 7, and each number represents a day on the week. For example, 1 represents Sunday, 2 represents Monday and so on. The user will type this number and the program will show them the name of the day on the week and if it's a business day or a weekend.

I tried to do it in a object-oriented way and using Python. Can someone please verify if my code is within the Python and object-oriented "standards"? Because i'm not sure if it is and i need a review.

Here's my code:

class DayOnWeek:
    ERROR_MESSAGE = 'Invalid weekday. Try again:'
    NUMBER_NAME_ASSOCIATIONS = {
        1: 'Sunday',
        2: 'Monday',
        3: 'Tuesday',
        4: 'Wednesday',
        5: 'Thursday',
        6: 'Friday',
        7: 'Saturday'
    }

    def __init__(self):
        self._day_number = 0
        self._day_name = None
        self._category = None

    @property
    def day_name(self):
        return self._day_name

    @property
    def category(self):
        return self._category

    @property
    def day_number(self):
        return self._day_number

    @day_number.setter
    def day_number(self, value):
        if 1 <= value <= 7:
            self._day_number = value
            self._day_name = DayOnWeek.NUMBER_NAME_ASSOCIATIONS[self._day_number]
        else:
            raise ValueError(DayOnWeek.ERROR_MESSAGE)

        if 2 <= value <= 6:
            self._category = 'Business day'
        else:
            self._category = 'Weekend'


class DayOnWeekInterface:

    def __init__(self):
        self._day_on_week = DayOnWeek()

    def request_day_number(self):
        while True:
            try:
                day_num = input('Type the number of the day on the week (ex: 1 - Sunday):')
                day_num = int(day_num)
                self._day_on_week.day_number = day_num
                break
            except ValueError:
                print(DayOnWeek.ERROR_MESSAGE)
        self.show_day_on_week()

    def show_day_on_week(self):
        day_num = self._day_on_week.day_number
        day_name = self._day_on_week.day_name
        day_category = self._day_on_week.category
        print(f'The day {day_num} is {day_name.lower()}')
        print(f'{day_name} is a {day_category.lower()}')


DayOnWeekInterface().request_day_number()