From dbd87068559b8503e00ba12690826f423e5d0339 Mon Sep 17 00:00:00 2001 From: "k.turkin" Date: Sat, 8 Mar 2025 12:28:15 +0300 Subject: [PATCH 1/2] Tasks 1--6 --- 1_if1.py | 20 +++++++++++++++++++- 2_if2.py | 37 ++++++++++++++++++++++++++++++++++++- 3_for.py | 36 +++++++++++++++++++++++++++++++++++- 4_while1.py | 10 +++++----- 5_while2.py | 18 ++++++++++++------ 6_exception1.py | 17 ++++++++++------- 6 files changed, 117 insertions(+), 21 deletions(-) diff --git a/1_if1.py b/1_if1.py index be736084..9aed6e4c 100644 --- a/1_if1.py +++ b/1_if1.py @@ -19,7 +19,25 @@ def main(): Эта функция вызывается автоматически при запуске скрипта в консоли В ней надо заменить pass на ваш код """ - pass + age = int(input('Введите ваш возраст: ')) + occupation = define_occupation(age) + print(occupation) + +def define_occupation(age): + + occupation = '' + if age < 6: + occupation = 'Детский сад' + elif age < 17: + occupation = 'Пора в школу' + elif age < 23: + occupation = 'Время института' + elif age < 70: + occupation = 'Иди работаать' + else: + occupation = 'Пора на пенсию' + + return occupation if __name__ == "__main__": main() diff --git a/2_if2.py b/2_if2.py index 0f1644f3..ff7b00e1 100644 --- a/2_if2.py +++ b/2_if2.py @@ -15,12 +15,47 @@ """ +from pprint import pprint def main(): """ Эта функция вызывается автоматически при запуске скрипта в консоли В ней надо заменить pass на ваш код """ - pass + + print("1, 'test_str' result:") + res = compare_two_vars(1, 'test_str') + pprint(res) + + print("'test_str', 'test_str' result:") + res = compare_two_vars('test_str', 'test_str') + pprint(res) + + print("'Loooooong', 'short' result:") + res = compare_two_vars('Loooooong', 'short') + pprint(res) + + print("'Python', 'learn' result:") + res = compare_two_vars('Python', 'learn') + pprint(res) + + +def compare_two_vars(var1, var2): + output = [] + + for i in range(4): + + if i == 0 and not all(isinstance(x, str) for x in [var1, var2]): + output.append (0) + if i == 1 and var1 == var2: + output.append (1) + if i == 2 and len(str(var1)) > len(str(var2)): + output.append (2) + if i == 3 and var1 != var2 and var2 == 'learn': + output.append (3) + + return output + + if __name__ == "__main__": main() diff --git a/3_for.py b/3_for.py index 5ca9f504..2977db4d 100644 --- a/3_for.py +++ b/3_for.py @@ -21,7 +21,41 @@ def main(): Эта функция вызывается автоматически при запуске скрипта в консоли В ней надо заменить pass на ваш код """ - pass + input_list = [ + {'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]}, + ] + for data_line in input_list: + the_sum = sum(data_line['items_sold']) + product = data_line['product'] + print(f'{product} total: {the_sum}') + + the_avg = avg(data_line['items_sold']) + print(f'{product} average: {the_avg}') + + + total_sells_list = merge_sells_data(input_list) + + total_sells_sum = sum(total_sells_list) + print (f'Total sells for all products: {total_sells_sum}') + + total_sells_average = avg(total_sells_list) + print (f'Average sells for all products: {total_sells_average}') + + +def avg(dataList): + return sum(dataList) / len(dataList) + + +def merge_sells_data(input_list): + + compound_list = [] + for data_line in input_list: + compound_list.extend(data_line['items_sold']) + + return compound_list + if __name__ == "__main__": main() diff --git a/4_while1.py b/4_while1.py index b5791517..b25ad6ac 100644 --- a/4_while1.py +++ b/4_while1.py @@ -11,11 +11,11 @@ def hello_user(): - """ - Замените pass на ваш код - """ - pass - + the_answer = input('How are you? ') + while the_answer.lower() not in ['good', 'fine', 'nice']: + the_answer = input('How are you? ') + + if __name__ == "__main__": hello_user() diff --git a/5_while2.py b/5_while2.py index 49012dfd..5d4efddc 100644 --- a/5_while2.py +++ b/5_while2.py @@ -15,13 +15,19 @@ """ -questions_and_answers = {} +questions_and_answers = {'How are you?' : 'All good', + 'What do you do?' : "I'm working", + "What's new?" : 'Started learning python'} -def ask_user(answers_dict): - """ - Замените pass на ваш код - """ - pass +def ask_user(answers_dict: dict): + + the_question = input('Talk to me: ') + + while the_question.lower() not in ['bye', 'stop', 'exit', 'finish']: + + the_answer = answers_dict.get(the_question, 'Ask something else') + print(the_answer) + the_question = input('Talk to me: ') if __name__ == "__main__": ask_user(questions_and_answers) diff --git a/6_exception1.py b/6_exception1.py index 3ea9d054..ba6b673b 100644 --- a/6_exception1.py +++ b/6_exception1.py @@ -4,17 +4,20 @@ Исключения: KeyboardInterrupt -* Перепишите функцию hello_user() из задания while1, чтобы она - перехватывала KeyboardInterrupt, писала пользователю "Пока!" - и завершала работу при помощи оператора break +* Перепишите функцию hello_user() из задания while1, чтобы она + перехватывала KeyboardInterrupt, писала пользователю "Пока!" + и завершала работу при помощи оператора break """ def hello_user(): - """ - Замените pass на ваш код - """ - pass + + the_answer = input('How are you? ') + while the_answer.lower() not in ['good', 'fine', 'nice']: + try: + the_answer = input('How are you? ') + except KeyboardInterrupt: + break if __name__ == "__main__": hello_user() From 191025287a4e29d8e5a33c4329a5940b12b5d140 Mon Sep 17 00:00:00 2001 From: "k.turkin" Date: Sun, 9 Mar 2025 12:41:28 +0300 Subject: [PATCH 2/2] Reverted to initial state --- 1_if1.py | 20 +------------------- 2_if2.py | 37 +------------------------------------ 3_for.py | 36 +----------------------------------- 4_while1.py | 10 +++++----- 5_while2.py | 18 ++++++------------ 6_exception1.py | 17 +++++++---------- 6 files changed, 21 insertions(+), 117 deletions(-) diff --git a/1_if1.py b/1_if1.py index 9aed6e4c..be736084 100644 --- a/1_if1.py +++ b/1_if1.py @@ -19,25 +19,7 @@ def main(): Эта функция вызывается автоматически при запуске скрипта в консоли В ней надо заменить pass на ваш код """ - age = int(input('Введите ваш возраст: ')) - occupation = define_occupation(age) - print(occupation) - -def define_occupation(age): - - occupation = '' - if age < 6: - occupation = 'Детский сад' - elif age < 17: - occupation = 'Пора в школу' - elif age < 23: - occupation = 'Время института' - elif age < 70: - occupation = 'Иди работаать' - else: - occupation = 'Пора на пенсию' - - return occupation + pass if __name__ == "__main__": main() diff --git a/2_if2.py b/2_if2.py index ff7b00e1..0f1644f3 100644 --- a/2_if2.py +++ b/2_if2.py @@ -15,47 +15,12 @@ """ -from pprint import pprint def main(): """ Эта функция вызывается автоматически при запуске скрипта в консоли В ней надо заменить pass на ваш код """ - - print("1, 'test_str' result:") - res = compare_two_vars(1, 'test_str') - pprint(res) - - print("'test_str', 'test_str' result:") - res = compare_two_vars('test_str', 'test_str') - pprint(res) - - print("'Loooooong', 'short' result:") - res = compare_two_vars('Loooooong', 'short') - pprint(res) - - print("'Python', 'learn' result:") - res = compare_two_vars('Python', 'learn') - pprint(res) - - -def compare_two_vars(var1, var2): + pass - output = [] - - for i in range(4): - - if i == 0 and not all(isinstance(x, str) for x in [var1, var2]): - output.append (0) - if i == 1 and var1 == var2: - output.append (1) - if i == 2 and len(str(var1)) > len(str(var2)): - output.append (2) - if i == 3 and var1 != var2 and var2 == 'learn': - output.append (3) - - return output - - if __name__ == "__main__": main() diff --git a/3_for.py b/3_for.py index 2977db4d..5ca9f504 100644 --- a/3_for.py +++ b/3_for.py @@ -21,41 +21,7 @@ def main(): Эта функция вызывается автоматически при запуске скрипта в консоли В ней надо заменить pass на ваш код """ - input_list = [ - {'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]}, - ] + pass - for data_line in input_list: - the_sum = sum(data_line['items_sold']) - product = data_line['product'] - print(f'{product} total: {the_sum}') - - the_avg = avg(data_line['items_sold']) - print(f'{product} average: {the_avg}') - - - total_sells_list = merge_sells_data(input_list) - - total_sells_sum = sum(total_sells_list) - print (f'Total sells for all products: {total_sells_sum}') - - total_sells_average = avg(total_sells_list) - print (f'Average sells for all products: {total_sells_average}') - - -def avg(dataList): - return sum(dataList) / len(dataList) - - -def merge_sells_data(input_list): - - compound_list = [] - for data_line in input_list: - compound_list.extend(data_line['items_sold']) - - return compound_list - if __name__ == "__main__": main() diff --git a/4_while1.py b/4_while1.py index b25ad6ac..b5791517 100644 --- a/4_while1.py +++ b/4_while1.py @@ -11,11 +11,11 @@ def hello_user(): - - the_answer = input('How are you? ') - while the_answer.lower() not in ['good', 'fine', 'nice']: - the_answer = input('How are you? ') - + """ + Замените pass на ваш код + """ + pass + if __name__ == "__main__": hello_user() diff --git a/5_while2.py b/5_while2.py index 5d4efddc..49012dfd 100644 --- a/5_while2.py +++ b/5_while2.py @@ -15,19 +15,13 @@ """ -questions_and_answers = {'How are you?' : 'All good', - 'What do you do?' : "I'm working", - "What's new?" : 'Started learning python'} +questions_and_answers = {} -def ask_user(answers_dict: dict): - - the_question = input('Talk to me: ') - - while the_question.lower() not in ['bye', 'stop', 'exit', 'finish']: - - the_answer = answers_dict.get(the_question, 'Ask something else') - print(the_answer) - the_question = input('Talk to me: ') +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 ba6b673b..3ea9d054 100644 --- a/6_exception1.py +++ b/6_exception1.py @@ -4,20 +4,17 @@ Исключения: KeyboardInterrupt -* Перепишите функцию hello_user() из задания while1, чтобы она - перехватывала KeyboardInterrupt, писала пользователю "Пока!" - и завершала работу при помощи оператора break +* Перепишите функцию hello_user() из задания while1, чтобы она + перехватывала KeyboardInterrupt, писала пользователю "Пока!" + и завершала работу при помощи оператора break """ def hello_user(): - - the_answer = input('How are you? ') - while the_answer.lower() not in ['good', 'fine', 'nice']: - try: - the_answer = input('How are you? ') - except KeyboardInterrupt: - break + """ + Замените pass на ваш код + """ + pass if __name__ == "__main__": hello_user()