diff --git a/1_if1.py b/1_if1.py index be736084..cd85ce90 100644 --- a/1_if1.py +++ b/1_if1.py @@ -14,12 +14,19 @@ """ -def main(): - """ - Эта функция вызывается автоматически при запуске скрипта в консоли - В ней надо заменить pass на ваш код - """ - pass +def main(age): + age = int(input('What is your age?')) + + if 3 <= age < 7: + return 'Вы должны быть в детском саду' + elif 7 <= age < 18: + return 'Вы должны быть в школе' + elif 18 <= age < 60: + return 'Вы должны быть на работе' + else: + return 'Отдыхайте!' + +result = main('age') if __name__ == "__main__": - main() + print(result) diff --git a/2_if2.py b/2_if2.py index 0f1644f3..fb06a583 100644 --- a/2_if2.py +++ b/2_if2.py @@ -15,12 +15,23 @@ """ -def main(): - """ - Эта функция вызывается автоматически при запуске скрипта в консоли - В ней надо заменить pass на ваш код - """ - pass +def main(str1, str2): + value1 = (isinstance(str1, str)) + value2 = (isinstance(str2, str)) + if value1 != True and value2 != True: + return '0' + elif str1 == str2: + return '1' + elif str1 != str2 and len(str1) > len(str2): + return '2' + elif str1 != str2 and str2 == 'python': + return '3' + + +print(main(1, 2)) +print(main('London', 'London')) +print(main('Winter is coming', 'Hear me roar!')) +print(main('poo', 'python')) + -if __name__ == "__main__": - main() + diff --git a/3_for.py b/3_for.py index 5ca9f504..4edfafee 100644 --- a/3_for.py +++ b/3_for.py @@ -16,12 +16,41 @@ * Посчитать и вывести среднее количество продаж всех товаров """ +stock = [ + {'product': 'iPhone 12', 'items_sold': [363, 500, 224, 358, 480, 476, 470, 216, 270, 388, 312, 186]}, + {'product': 'Xiaomi Mi11', 'items_sold': [317, 267, 290, 431, 211, 354, 276, 526, 141, 453, 510, 316]}, + {'product': 'Samsung Galaxy 21', 'items_sold': [343, 390, 238, 437, 214, 494, 441, 518, 212, 288, 272, 247]}, + ] + def main(): - """ - Эта функция вызывается автоматически при запуске скрипта в консоли - В ней надо заменить pass на ваш код - """ - pass + def item_sum(quantity): + sold_sum = 0 + for item in quantity: + sold_sum += item + return sold_sum + +all_prod_sum = 0 +for one_product in stock: + product_sum = product_sum(one_product['items_sold']) + print(product_sum) + all_prod_sum += product_sum + print(all_prod_sum) + + +def item_avg(quantity): + sell_sum = 0 + for item in quantity: + sell_sum += item + avg_prod = sell_sum / len(quantity) + return avg_prod + +all_prod_avg = 0 +for one_product in stock: + product_avg = item_avg(one_product['items_sold']) + print(product_avg) + +all_prod_avg = product_avg / len(stock) +print(all_prod_avg) if __name__ == "__main__": main() diff --git a/4_while1.py b/4_while1.py index b5791517..0d31a04d 100644 --- a/4_while1.py +++ b/4_while1.py @@ -11,10 +11,13 @@ def hello_user(): - """ - Замените pass на ваш код - """ - pass + while True: + user_word = input('What does the fox say?') + if user_word == 'furfur': + print('oooooooh, so sweet!') + break + else: + print('Am I a joke to you?') if __name__ == "__main__": diff --git a/5_while2.py b/5_while2.py index 49012dfd..e627be3f 100644 --- a/5_while2.py +++ b/5_while2.py @@ -15,13 +15,22 @@ """ -questions_and_answers = {} +questions_and_answers = { + 'How are you?': 'How are you', + 'How old are you?': 'Next question :)', + 'Do you like cats?': 'Im obsessed with this sweet fury creatures' +} + + +def ask_user(questions_and_answers): + while True: + start = input('Ask a question: ') + answer = questions_and_answers.get(start, "wtf") + print(answer) + + +ask_user(questions_and_answers) -def ask_user(answers_dict): - """ - Замените pass на ваш код - """ - pass if __name__ == "__main__": ask_user(questions_and_answers) diff --git a/6_exception1.py b/6_exception1.py index 3ea9d054..8ce4ac6b 100644 --- a/6_exception1.py +++ b/6_exception1.py @@ -11,10 +11,16 @@ """ def hello_user(): - """ - Замените pass на ваш код - """ - pass + while True: + try: + user_word = input('What does the fox say?') + if user_word == 'furfur': + print('oooooooh, so sweet!') + else: + print('Am I a joke to you?') + except KeyboardInterrupt: + print('Bye!') + break if __name__ == "__main__": - hello_user() + hello_user() \ No newline at end of file diff --git a/7_exception2.py b/7_exception2.py index d4bd8a39..d34af1ff 100644 --- a/7_exception2.py +++ b/7_exception2.py @@ -12,14 +12,21 @@ ValueError и TypeError, если приведение типов не сработало. """ - -def discounted(price, discount, max_discount=20) - """ - Замените pass на ваш код - """ - pass +def discounted(price, discount, max_discount=20): + try: + price = abs(float(price)) + discount = abs(float(discount)) + max_discount = abs(int(max_discount)) + except(ValueError, TypeError): + if max_discount >= 100: + raise ValueError('Слишком большая максимальная скидка') + if discount >= max_discount: + return price + else: + return price - (price * discount / 100) if __name__ == "__main__": + print(discounted(100, 2)) print(discounted(100, "3")) print(discounted("100", "4.5")) diff --git a/8_ephem_bot.py b/8_ephem_bot.py index 1cf9ea19..4514885b 100644 --- a/8_ephem_bot.py +++ b/8_ephem_bot.py @@ -21,13 +21,7 @@ filename='bot.log') -PROXY = { - 'proxy_url': 'socks5://t1.learn.python.ru:1080', - 'urllib3_proxy_kwargs': { - 'username': 'learn', - 'password': 'python' - } -} + def greet_user(update, context): @@ -43,7 +37,7 @@ def talk_to_me(update, context): def main(): - mybot = Updater("КЛЮЧ, КОТОРЫЙ НАМ ВЫДАЛ BotFather", request_kwargs=PROXY, use_context=True) + mybot = Updater("КЛЮЧ, КОТОРЫЙ НАМ ВЫДАЛ BotFather", use_context=True) dp = mybot.dispatcher dp.add_handler(CommandHandler("start", greet_user)) @@ -54,4 +48,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file