{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Домашнее задание 5: решения творческих задач\n", "\n", "### Задача 6" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "answers = {\"She turned ___ the light so as to enjoy the darkness.\" : \"off\",\n", " \"It was so hot in the room, so she felt she was going to get ___.\" : \"down\",\n", " \"Mary turned right and came ___ her friend Peter.\" : \"across\", \n", " \"It will be very cold in the mountains, so put ___ all warm clothes you have.\" : \"on\", \n", " \"There was no morning when he did not come ___ with a great idea.\" : \"up\", \n", " \"I can't hear you properly, speak ___, please.\" : \"up\", \n", " \"Give ___ talking when I start to speak.\" : \"up\",\n", " \"We get ___ well with our neighbours, we never argue.\" : \"on\", \n", " \"Be careful, the plane is going to take ___.\" : \"off\",\n", " \"He was very sad while listening to the teacher who called him ___.\" : \"down\"}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Fill in the gaps in the sentences.\n", "\n", "She turned ___ the light so as to enjoy the darkness.\n", "Your answer: off\n", "It was so hot in the room, so she felt she was going to get ___.\n", "Your answer: down\n", "Mary turned right and came ___ her friend Peter.\n", "Your answer: across\n", "It will be very cold in the mountains, so put ___ all warm clothes you have.\n", "Your answer: on\n", "There was no morning when he did not come ___ with a great idea.\n", "Your answer: up\n", "I can't hear you properly, speak ___, please.\n", "Your answer: up\n", "Give ___ talking when I start to speak.\n", "Your answer: up\n", "We get ___ well with our neighbours, we never argue.\n", "Your answer: up\n", "Be careful, the plane is going to take ___.\n", "Your answer: up\n", "He was very sad while listening to the teacher who called him ___.\n", "Your answer: off\n", "\n", "Your score is: 7 out of 10.\n", "\n", "You made mistakes in the following sentences:\n", "\n", "We get ___ well with our neighbours, we never argue.\n", "Be careful, the plane is going to take ___.\n", "He was very sad while listening to the teacher who called him ___.\n" ] } ], "source": [ "print(\"Fill in the gaps in the sentences.\\n\")\n", "score = 0\n", "errors = []\n", "\n", "for k, v in answers.items():\n", " print(k)\n", " attempt = input(\"Your answer: \")\n", " if attempt == v:\n", " score = score + 1\n", " else:\n", " errors.append(k)\n", "print(\"\\nYour score is: %i out of 10.\\n\"%score)\n", "\n", "if score < 10:\n", " print(\"You made mistakes in the following sentences:\\n\")\n", " for e in errors:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Задача 7" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Решите пример:\n", " 4^2+3*8\n", "40\n", "Верно!\n", "Решите пример:\n", " 25+4^3*5\n", "300\n", "Попробуйте еще раз!\n", " 25+4^3*5\n", "345\n", "Верно!\n", "Решите пример:\n", " 36^0.5+12\n", "18\n", "Верно!\n", "Решите пример:\n", " 34-12*3^2\n", "-126\n", "Попробуйте еще раз!\n", " 34-12*3^2\n", "-74\n", "Верно!\n", "Решите пример:\n", " 2^3+16^0.5-1\n", "11\n", "Верно!\n", "Решите пример:\n", " 8*0-2^8\n", "-256\n", "Верно!\n", "Вы решили все примеры! Идите пить чай!\n" ] } ], "source": [ "exer = [\"4^2+3*8\", \"25+4^3*5\", \"36^0.5+12\", \n", " \"34-12*3^2\", \"2^3+16^0.5-1\", \"8*0-2^8\"]\n", "exer_py = [e.replace('^','**') for e in exer] # заменяем на питоновские ** для степени\n", "answers = [eval(e) for e in exer_py] # исполняем код в кавычках, чтобы сразу получить ответы\n", "with_answers = dict(zip(exer, answers)) # склеиваем вопрос-ответ\n", "\n", "for primer, answer in with_answers.items():\n", " print(\"Решите пример:\\n\", primer)\n", " attempt = int(input())\n", " \n", " while True:\n", " if attempt != answer:\n", " print(\"Попробуйте еще раз!\\n\", primer)\n", " attempt = int(input())\n", " else:\n", " print(\"Верно!\")\n", " break\n", "\n", "print(\"Вы решили все примеры! Идите пить чай!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Решение выше более общее, то есть, если не знать про `eval()` для исполнения кода, заданного в виде строки, можно создать список ответов вручную и потом составить словарь соответствий пример-ответ. Если знать про `eval()`, то решение можно упростить и обойтись без словаря соответствий:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exer = [\"4^2+3*8\", \"25+4^3*5\", \"36^0.5+12\", \n", " \"34-12*3^2\", \"2^3+16^0.5-1\", \"8*0-2^8\"]\n", "exer_py = [e.replace('^','**') for e in exer]\n", "\n", "for primer in exer_py:\n", " print(\"Решите пример:\\n\", primer)\n", " attempt = int(input())\n", " while True:\n", " if attempt != eval(primer):\n", " print(\"Попробуйте еще раз!\\n\", primer)\n", " attempt = int(input())\n", " else:\n", " print(\"Верно!\")\n", " break\n", "\n", "print(\"Вы решили все примеры! Идите пить чай!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Задача 8" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['вышка', 'сессия', 'словарь']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# список слов из файла words.txt\n", "word_list = []\n", "with open('words.txt') as f: # or whatever the wordlist is saved as\n", " for line in f.readlines():\n", " word_list.append(line.strip())\n", "word_list" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'вышка'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# выбор случайного слова - используйте вторую строчку для выбора слова\n", "from random import choice\n", "rword = choice(word_list)\n", "rword" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'в _ _ _ а'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# i - номер символа, до которого отображаются начальные буквы слова \n", "# (сначала первая, потом первые две, потом перые три и т д)\n", "# n - число пропусков _\n", "\n", "i = 1 \n", "n = len(rword) - 2\n", "hidden = rword[0:i] + ' _ ' * n + rword[-1]\n", "hidden" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Угадайте слово: в _ _ _ а\n", "Введите слово: вилка\n", "Подсказка: вы _ _ а\n", "Введите слово: вы\n", "Подсказка: выш _ а\n", "Введите слово: вышка\n", "Так держать!\n" ] } ], "source": [ "print(\"Угадайте слово:\", hidden)\n", "attempt = input(\"Введите слово: \")\n", "while attempt != rword:\n", " i = i + 1 # выводим больше букв\n", " n = n - 1 # и меньше пропусков\n", " hidden = rword[0:i] + ' _ ' * n + rword[-1]\n", " print(\"Подсказка: \", hidden)\n", " attempt = input(\"Введите слово: \")\n", "print(\"Так держать!\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }