Дефолт аргумент

def say_hello(current_hour, name=''):
    if current_hour <= 5 or current_hour >= 23:
        hello_message = 'Доброй ночи'
    elif current_hour >= 6 and current_hour <= 11:
        hello_message = 'Доброе утро'
    elif current_hour >= 12 and current_hour <= 17:
        hello_message = 'Добрый день'
    elif current_hour >= 18 and current_hour <= 22:
        hello_message = 'Добрый вечер'
    if name != '':
        print(hello_message + ', ' + name + '!')
    else:
        print(hello_message + '!')


say_hello(4)

Хочу сделать дефолтный аргумент для current_hour так, чтобы? если поле say_hello() было пустым, выводило бы сообщение "Привет." Столкнулся с проблемой: если делать это "в лоб": def say_hello(current_hour="", name=''):, ошибки srt int, как сделать все красиво?


Ответы (1 шт):

Автор решения: eri
def say_hello(current_hour=None, name=''):
    if current_hour is None:
        hello_message = 'Привет'
    elif current_hour <= 5 or current_hour >= 23:
        hello_message = 'Доброй ночи'
    elif current_hour >= 6 and current_hour <= 11:
        hello_message = 'Доброе утро'
    elif current_hour >= 12 and current_hour <= 17:
        hello_message = 'Добрый день'
    elif current_hour >= 18 and current_hour <= 22:
        hello_message = 'Добрый вечер'
    if name != '':
        print(hello_message + ', ' + name + '!')
    else:
        print(hello_message + '!')


say_hello(4)
→ Ссылка