3.3.3. Practice: Statements and Variables#

Choose Social Media Platform: Reddit | Discord | Bluesky | No Coding

This Python Notebook is a chance for you to try out the programming concepts we have covered thus far.

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”

screenshot of this page in the online textbook, with the launch binder link highlighted under the rocket button at the top.

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

a screenshot of this page viewed in jupyter_lab, with menus and options above the editable page

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).

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

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.

So now you can go through the rest of this page and try out the practice exercises for yourself!

Variables#

You will first practice saving values into variables. Remember, the way we save a value into a variable is like this:

variable_name = value

First, save the piece of text “I am writing a computer program!” into a variable called my_progress

# TODO: enter your code here

Viewing variables in the debugger#

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.

On the tp right of this tab, press the small bug icon to “enable debugging”:

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)

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:

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"

Practice number variables#

First, write and run a line of code to save the value 5 into a variable named number_of_pies

# TODO: enter your code here

Now, save the value 12.5 into a variable named cost_per_pie

# TODO: enter your code here

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.

Note: In python (and many programming languages), the symbol for multiply is *

# TODO: enter your code here

Now use the display function to display what is saved in total_pie_cost

# TODO: enter your code here

More variable practice#

Now, make a new variable called first_name and assign your first name to it

# TODO: enter your code here

Now, make a variable calles last_name and save your last name to it

# TODO: enter your code here

Create a variable called age and assign your age to it.

# TODO: enter your code here

~ A year goes by ~

Increase the age variable by 1.

# TODO: enter your code here

Now write three lines of code, with each line using display to show what is saved in first_name, last_name, and `age

# TODO: enter your code here

Sleep#

In order to use sleep, we must first import it from the time library

from time import sleep

Now try displaying 5 messages of your choosing, with some pauses between each one:

# TODO: enter your code here

Reddit Bot Practice#

Now lets try a Reddit bot with variables and sleep!

Load Praw Library#

First, we need to load the praw library

# Load some code called "praw" that will help us work with reddit
import praw

(Optional) Step 1b: Make a fake praw connection with the fake_praw library#

For testing purposes, we’ve added this line of code, which loads a fake version of praw, so it wont actually connect to reddit. If you want to try to actually connect to reddit, don’t run this line of code.

%run ../../fake_apis/fake_praw.ipynb
Fake praw is replacing the praw library. Fake praw doesn't need real passwords, and prevents you from accessing real reddit

Step 2: Load your developer access passwords#

To use this on your real Reddit account, copy your developer access passwords into the code below, replacing our fake passwords.

# Load all your developer access passwords into Python
# TODO: Put your reddit username, password, and special developer access passwords below:
username="fake_reddit_username"
password="sa@#4*fdf_fake_password_$%DSG#%DG"
client_id="45adf$TW_fake_client_id_JESdsg1O"
client_secret="56sd_fake_client_secret_%Yh%"

Step 4: Give praw (or fake_praw) your developer access passwords#

# Give the praw code your reddit account info so
# it can perform reddit actions
reddit = praw.Reddit(
    username=username, password=password,
    client_id=client_id, client_secret=client_secret,
    user_agent="a custom python script"
)
Fake praw is pretending to collect account info to use on reddit

Practice 1: Submit a post to Reddit#

Post something you learned in the class so far:

Remember, the code to post a tweet looks like this:

reddit.subreddit(
   "soc_media_ethics_auto"
).submit(
   "This is the title of the post", 
   selftext = "This is the content of the post"
)
# TODO: enter your code here

Practice 2: Post from a variable#

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.

To do this, where the code has submit( ... ), you’ll replace the quoted text with the variable name, so it will look like:

reddit.subreddit(
   "soc_media_ethics_auto"
).submit(
   title_variable, 
   selftext = content_variable
)

(with whatever your variable name was instead of “title_variable” and “content_variable”)

# TODO: enter your code here

Practice 3: Submit multiple posts#

Next try submitting 3 posts, but use sleep to add pauses between each one (note that reddit might not want you to post too often so you can sleep for a few seconds or minutes between each time).

Practice 3: Submit multiple posts#

Next try submitting 3 posts, but use sleep to add pauses between each one (note that reddit might not want you to post too often so you can sleep for a few seconds or minutes between each time).

# TODO: enter your code here