diff --git a/1_if1.py b/1_if1.py index 202a6698..98e4a9d1 100644 --- a/1_if1.py +++ b/1_if1.py @@ -17,7 +17,9 @@ def main(): result = '' - age = abs(int(input('Введите возраст пользователя: '))) + age = int(input('Введите возраст пользователя: ')) + if age < 0: + result = 'Возраст не может быть отрицательным' if 65 > age >= 23: result = 'Скорее всего пользователь работает' elif 23 > age >= 18: @@ -28,8 +30,8 @@ def main(): result = 'Скорее всего пользователь ходит в детский сад' else: result = 'Неизвестно что делает пользователь в этом возрасте' - return print(result) + return result if __name__ == "__main__": - main() + print(main()) diff --git a/2_if2.py b/2_if2.py index 4c8a2908..990f1927 100644 --- a/2_if2.py +++ b/2_if2.py @@ -15,15 +15,17 @@ """ + def main(first_str: str, second_str: str): - if type(first_str) is not str and type(second_str) is not str: - return print(0) + if not isinstance(first_str, str) and not isinstance(second_str, str): + return 0 elif first_str is not second_str and second_str == 'learn': - return print(3) + return 3 elif first_str is not second_str and len(first_str) > len(second_str): - return print(2) - + return 2 + + if __name__ == "__main__": - main(1,2) - main('gun', 'gun') - main('easy', 'learn') + print(main(1,2)) + print(main('gun', 'gun')) + print(main('easy', 'learn')) diff --git a/3_for.py b/3_for.py index 965edbf8..3ca1dd1c 100644 --- a/3_for.py +++ b/3_for.py @@ -31,14 +31,14 @@ def school_average(school: list): for list_of_scores in school: result += (sum(list_of_scores['score']) / len(list_of_scores['score'])) result = result / len(school) - print('Average mark of school is:', '%.2f' % result) + print('Average mark of school is:', f'{result:.2f}') def class_average(school: list): result = 0 for list_of_scores in school: result = sum(list_of_scores['score']) / len(list_of_scores['score']) - print('Average for class ', list_of_scores['school_class'], 'is:', '%.2f' % result) + print('Average for class ', list_of_scores['school_class'], 'is:', f'{result:.2f}') def main(school: list): diff --git a/5_while2.py b/5_while2.py index 1c95dc33..52d13a9f 100644 --- a/5_while2.py +++ b/5_while2.py @@ -27,8 +27,9 @@ def ask_user(answers: dict): console_input = '' while console_input != 'Пока': console_input = input('Введите вопрос: ') - if answers.get(console_input): - print(answers.get(console_input)) + check_answers = answers.get(console_input) + if check_answers: + print(check_answers) elif console_input != 'Пока': print('Я затрудняюсь ответить на вопрос') diff --git a/6_exception1.py b/6_exception1.py index ae86c50d..f9f27f2a 100644 --- a/6_exception1.py +++ b/6_exception1.py @@ -21,8 +21,9 @@ def ask_user(answers: dict): while console_input != 'Пока': try: console_input = input('Введите вопрос: ') - if answers.get(console_input): - print(answers.get(console_input)) + check_answers = answers.get(console_input) + if check_answers: + print(check_answers) elif console_input != 'Пока': print('Я затрудняюсь ответить на вопрос') except KeyboardInterrupt: diff --git a/7_exception2.py b/7_exception2.py index c1400c97..8f7aa5fb 100644 --- a/7_exception2.py +++ b/7_exception2.py @@ -16,11 +16,11 @@ def get_summ(num_one, num_two): try: result = int(num_one) + int(num_two) - return result except ValueError: result = 'На вход функция может принимать только занчения типа int' - return result - + return result + + if __name__ == "__main__": print(get_summ(2, 2)) print(get_summ(3, "3")) diff --git a/8_ephem_bot.py b/8_ephem_bot.py index 88b5141e..c4f3383d 100644 --- a/8_ephem_bot.py +++ b/8_ephem_bot.py @@ -44,26 +44,24 @@ def talk_to_me(bot, update): def find_planet_place(update, context): + planet_dict = { + 'Mars': ephem.Mars, + 'Jupiter': ephem.Jupiter, + 'Mercury': ephem.Mercury, + 'Venus': ephem.Venus, + 'Saturn': ephem.Saturn, + 'Uranus': ephem.Uranus, + 'Neptune': ephem.Neptune, + } dt_now = ephem.now() get_planet = update.message.text.split() - if get_planet[1] == 'Mars': - planet_name = ephem.Mars(dt_now) - if get_planet[1] == 'Jupiter': - planet_name = ephem.Jupiter(dt_now) - if get_planet[1] == 'Mercury': - planet_name = ephem.Mercury(dt_now) - if get_planet[1] == 'Venus': - planet_name = ephem.Venus(dt_now) - if get_planet[1] == 'Saturn': - planet_name = ephem.Saturn(dt_now) - if get_planet[1] == 'Uranus': - planet_name = ephem.Uranus(dt_now) - if get_planet[1] == 'Neptune': - planet_name = ephem.Neptune(dt_now) + if planet_dict.get(get_planet[1]): + planet_name = planet_dict.get(get_planet[1]) + planet_name = planet_name(dt_now) + planet_constellation = ephem.constellation(planet_name) + update.message.reply_text(planet_constellation[1]) else: update.message.reply_text('Про этот объект я ничего не знаю') - planet_constellation = ephem.constellation(planet_name) - update.message.reply_text(planet_constellation[1]) def main():