{ "cells": [ { "cell_type": "markdown", "id": "a5538971-c471-42ab-bcb6-6d75a714b287", "metadata": {}, "source": [ "# Practice: Statements and Variables" ] }, { "cell_type": "markdown", "id": "123456789-930485093240532940945-0324095320945904325", "metadata": { "tags": [] }, "source": [" _Choose Social Media Platform: Reddit | Discord | __Bluesky__ | No Coding_ "] }, { "cell_type": "markdown", "id": "5323da86-18f1-4557-8f82-0b58581f289b", "metadata": {}, "source": [ "This Python Notebook is a chance for you to try out the programming concepts we have covered thus far.\n", "\n", "As we mentioned previously in the first bot demo (2.3.8), in order to run the code, you can look for the rocket button at the top which will give you an option to \"launch binder\"\n", "\n", "![screenshot of this page in the online textbook, with the launch binder link highlighted under the rocket button at the top.](binder_link.png)\n", "\n", "If you launch binder, it will take a while to load, but eventually show you a version of this page Jupyter Notebook in a code editor called Jupyter Lab\n", "\n", "![a screenshot of this page viewed in jupyter_lab, with menus and options above the editable page](jupyter_lab.png)\n", "\n", "In Jupyter Lab you can double click any section to edit it, and you can press the triangle \"run\" button to run the code (or display the text).\n", "\n", "![a screenshot of this page viewed in jupyter_lab,with the triangle \"run\" button circled. Next to it are a square \"interrupt the kernal\" button and other options](jupyter_run_code.png)\n", "\n", "When the code runs, the little number to the left of the code block should change. There might also be some output from your action displyed below the code block.\n", "\n", "So now you can go through the rest of this page and try out the practice exercises for yourself!\n" ] }, { "cell_type": "markdown", "id": "152fc92f-dcb2-40c8-bd0a-73b2592a42d6", "metadata": { "tags": [] }, "source": [ "## Variables\n", "You will first practice saving values into variables. Remember, the way we save a value into a variable is like this:\n", "```\n", "variable_name = value\n", "```\n", "\n", "First, save the piece of text \"I am writing a computer program!\" into a variable called `my_progress`" ] }, { "cell_type": "code", "execution_count": 2, "id": "ba392144-c5d0-4bcf-8682-aaac3f72fae2", "metadata": {}, "outputs": [], "source": [ "# TODO: enter your code here" ] }, { "cell_type": "markdown", "id": "6a9381ee-40e9-471e-880f-3d56c21528b5", "metadata": {}, "source": [ "### Viewing variables in the debugger\n", "Before we continue, we are going to show you how to open the debugger so you can see what is being saved in your variables.\n", "\n", "On the tp right of this tab, press the small bug icon to \"enable debugging\":\n", "\n", "![screenshot of this tab in jupyterhub, with the \"enable debugging\" icon circled at the top right (after the \"git\" option and before the \"Python 3 (ipykernel)\" option)](debugging_icon.png)\n", "\n", "Then, if you did the step above correctly, you should see the variable `my_progress` with the value \"I am writing a computer program!\" next to it:\n", "\n", "![screenshot of this tab in jupyterhub, with the debugger tab opened on the right. The top section of that tab is variables, which has \"special variables\" then \"function variables\" then \"my_progress:I am writing a computer program\"](debugger_vars.png)" ] }, { "cell_type": "markdown", "id": "bec48c5c-eb60-4e68-93be-136505594697", "metadata": {}, "source": [ "### Practice number variables\n", "First, write and run a line of code to save the value 5 into a variable named `number_of_pies`" ] }, { "cell_type": "code", "execution_count": null, "id": "bc4cfc07-a59a-4ad3-959d-824461511e9e", "metadata": {}, "outputs": [], "source": [ "# TODO: enter your code here" ] }, { "cell_type": "markdown", "id": "6cd44367-b94d-48bc-abbb-dbab35d070f3", "metadata": {}, "source": [ "Now, save the value 12.5 into a variable named `cost_per_pie`" ] }, { "cell_type": "code", "execution_count": null, "id": "c5483b8c-ef40-4a5d-929b-d7e5ec71dada", "metadata": {}, "outputs": [], "source": [ "# TODO: enter your code here" ] }, { "cell_type": "markdown", "id": "82eae4e8-37f5-4cd4-8545-210e5007fa8f", "metadata": {}, "source": [ "Now make a new variable called `total_pie_cost` and save into the value of the number_of_pies multiplied by the cost_per_pie.\n", "\n", "Note: In python (and many programming languages), the symbol for multiply is `*`" ] }, { "cell_type": "code", "execution_count": null, "id": "fe3cbc70-d19f-45fd-badf-769d13ece4e6", "metadata": {}, "outputs": [], "source": [ "# TODO: enter your code here" ] }, { "cell_type": "markdown", "id": "5576d9c6-a1da-4091-b400-3158a08616cc", "metadata": {}, "source": [ "Now use the `display` function to display what is saved in total_pie_cost" ] }, { "cell_type": "code", "execution_count": null, "id": "76cf6997-4ff8-40c7-a9a9-36b8615159dd", "metadata": {}, "outputs": [], "source": [ "# TODO: enter your code here" ] }, { "cell_type": "markdown", "id": "7b69ec96-f795-4705-8e67-0ea53f83146b", "metadata": {}, "source": [ "### More variable practice\n", "\n", "Now, make a new variable called `first_name` and assign your first name to it" ] }, { "cell_type": "code", "execution_count": null, "id": "e6365b43-516a-4029-8eeb-31d6897281de", "metadata": {}, "outputs": [], "source": [ "# TODO: enter your code here" ] }, { "cell_type": "markdown", "id": "eeecbbbf-5cb2-4015-b9a2-59881e539552", "metadata": {}, "source": [ "Now, make a variable calles `last_name` and save your last name to it" ] }, { "cell_type": "code", "execution_count": null, "id": "408e7e01-4497-49e6-87a6-bc1f8f37c76f", "metadata": {}, "outputs": [], "source": [ "# TODO: enter your code here" ] }, { "cell_type": "markdown", "id": "bfda3c2d-323f-4fee-b883-42e7fcb4365e", "metadata": {}, "source": [ "Create a variable called `age` and assign your age to it." ] }, { "cell_type": "code", "execution_count": null, "id": "c48e68c7-bc8d-4dc1-921e-227cc26ef122", "metadata": {}, "outputs": [], "source": [ "# TODO: enter your code here" ] }, { "cell_type": "markdown", "id": "cfb2e77b-0eb1-4abf-87f6-826f04b10ca0", "metadata": {}, "source": [ "~ A year goes by ~\n", "\n", "Increase the `age` variable by 1. " ] }, { "cell_type": "code", "execution_count": null, "id": "471d4448-d39a-4601-b3f4-94e47d4f20fd", "metadata": {}, "outputs": [], "source": [ "# TODO: enter your code here" ] }, { "cell_type": "markdown", "id": "3806aaca-9c52-43e7-af56-c87c96b567eb", "metadata": {}, "source": [ "Now write three lines of code, with each line using `display` to show what is saved in `first_name`, `last_name`, and `age " ] }, { "cell_type": "code", "execution_count": null, "id": "cc94033a-16b6-4c01-b90c-0bc9bed7c66f", "metadata": {}, "outputs": [], "source": [ "# TODO: enter your code here" ] }, { "cell_type": "markdown", "id": "c3dc6e7f-63e4-4975-92cf-1042dc3294c6", "metadata": { "tags": [] }, "source": [ "## Sleep\n", "In order to use sleep, we must first import it from the time library" ] }, { "cell_type": "code", "execution_count": 9, "id": "d9e4f8d6-a4a0-441b-aafa-a98d62ce23ab", "metadata": {}, "outputs": [], "source": [ "from time import sleep" ] }, { "cell_type": "markdown", "id": "25b8da3f-dc18-42be-a5a4-c013eede8be5", "metadata": {}, "source": [ "Now try displaying 5 messages of your choosing, with some pauses between each one:" ] }, { "cell_type": "code", "execution_count": null, "id": "25335e93-d48b-472b-b4be-86d237aa6282", "metadata": {}, "outputs": [], "source": [ "# TODO: enter your code here" ] }, { "cell_type": "markdown", "id": "e48c9d68-e85e-4a46-9f52-472192bdfd18", "metadata": {}, "source": [ "## Bluesky Bot Practice\n", "Now lets try a Bluesky bot with variables and sleep!\n", "\n", "### Step 1: Load atproto (Bluesky) code\n", "First, we need to load the atproto library" ] }, { "cell_type": "code", "execution_count": 3, "id": "0d001424-fb48-4f28-9924-2a80c6dc7a5b", "metadata": {}, "outputs": [], "source": [ "# Load some code called \"Client\" from the \"atproto\" library that will help us work with Bluesky\n", "from atproto import Client" ] }, { "cell_type": "markdown", "id": "57aca520-4b67-4bcf-bc2c-a09b8adb4c3f", "metadata": {}, "source": [ "### (Optional) Step 1b: Make a fake Bluesky connection with the fake_atproto library\n", "For testing purposes, we\"ve added this line of code, which loads a fake version of atproto, so it wont actually connect to Bluesky. __If you want to try to actually connect to Bluesky, don't run this line of code.__" ] }, { "cell_type": "code", "execution_count": 1, "id": "d116f0ad-b143-4cf3-9020-8f72e7bf1cb8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "Fake tweepy is replacing the tweepy library. Fake Tweepy doesn't need real passwords, and prevents you from accessing real twitter" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%run ../../fake_apis/fake_atproto.ipynb" ] }, { "cell_type": "markdown", "id": "641e5fc1-6634-41bf-a499-21381be6b1e6", "metadata": {}, "source": [ "### Step 2: Login to Bluesky\n", "To use this on your real Bluesky account, copy your [bluesky account name and login](../../appendix/making_bot_account.md) into the code below, replacing our fake bluesky name and password." ] }, { "cell_type": "code", "execution_count": 5, "id": "ed6b57e6-17a7-4b7c-ad59-6d1b39e5875b", "metadata": {}, "outputs": [], "source": [ "# Login to Bluesky\n", "# TODO: put your account name and password below\n", "\n", "client = Client(base_url=\"https://bsky.social\")\n", "client.login(\"your_account_name.bsky.social\", \"m#5@_fake_bsky_password_$%Ds\")" ] }, { "cell_type": "markdown", "id": "ca34a3b4-f543-4ebe-abe5-3f624ee2754a", "metadata": {}, "source": [ "### Practice 1: Submit a post to Bluesky\n", "Post something you learned in the class so far:\n", "\n", "Remember, the code to post to Bluesky looks like this: \n", "```\n", "client.send_post(\"This post was made by a computer program!\")\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "54c02053-001d-49d2-8a55-5e979e303dbb", "metadata": {}, "outputs": [], "source": [ "# TODO: enter your code here" ] }, { "cell_type": "markdown", "id": "98871ebf-1238-40af-8021-c73ae7a6c3e1", "metadata": {}, "source": [ "### Practice 2: Post from a variable\n", "Now try saving a piece of text in a variable for the title, and another for the content of a post, and then submitting a post of whatever you saved in the variables. \n", "\n", "To do this, where the code has `send_post( ... )`, you'll replace the quoted text with the variable name, so it will look like:\n", "```\n", "client.send_post(content_variable)\n", "``` \n", "(with whatever your variable name was instead of \"content_variable\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "af5757f6-c84a-4b1c-ac0a-d2062cc0f8e5", "metadata": {}, "outputs": [], "source": [ "# TODO: enter your code here" ] }, { "cell_type": "markdown", "id": "6712d8c5-0aee-4cfc-acfa-81b066d10d82", "metadata": {}, "source": [ "### Practice 3: Submit multiple posts\n", "Next try submitting 3 posts, but use `sleep` to add pauses between each one (note that Bluesky might not want you to post too often so you can sleep for a few seconds or minutes between each time)." ] }, { "cell_type": "code", "execution_count": null, "id": "5f971a63-0e44-4a99-ba28-e9660d9bce28", "metadata": {}, "outputs": [], "source": [ "# TODO: enter your code here" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.4" }, "vscode": { "interpreter": { "hash": "369f2c481f4da34e4445cda3fffd2e751bd1c4d706f27375911949ba6bb62e1c" } } }, "nbformat": 4, "nbformat_minor": 5 }