{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A bot in the shell" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ ">>> hello\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "<<< oh hello!\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ ">>> and this\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "<<< What did you say?\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ ">>> hello\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "<<< oh hello!\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ ">>> yes i don't know what to say\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "<<< What did you say?\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ ">>> hello\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "<<< oh hello!\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ ">>> exit\n" ] } ], "source": [ "def get_reply(message):\n", " if \"hello\" in message:\n", " reply = \"oh hello!\"\n", " else:\n", " reply = None\n", "\n", " return reply\n", " \n", "while True:\n", " message = input(\">>>\")\n", " if message == \"exit\":\n", " break\n", " \n", " reply = get_reply(message)\n", " \n", " if reply: \n", " print(\"<<<\", reply)\n", " else:\n", " print(\"<<<\", \"What did you say?\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Run the bot from a terminal\n", "\n", "You can also save this code as a `.py` script, and run it from the terminal.\n", "\n", "1. Make a new file and save it as a `.py` script\n", "2. Open a new terminal \n", "3. Navigate to the folder where you saved your `.py` script\n", "4. Run your bot: `$ python3 YOURFILENAME.py`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }