Skip to content
Bell Codo edited this page Aug 26, 2016 · 4 revisions

Building an activity planner

  • To add mystery and magic to the plan begin in a new repo in github.
  • Create a file code_activities_we_like.lst
  • Brainstorm a list one item per line no spaces
codecombat
scratch
tynker
codeacademy
10fastfingers.com
typing.lk
typing.com/games
typingclub.com
  • Commit the file.
  • Navigate to codeskulptor.
  • Select all the inner welcome code and delete it.
  • Template out the steps to a new activity_picker.py application.

activity_picker.py

# import the magic random picker

# list of things to pick from

# find a way to pick something

# a way to tell the user what to do
  • Each of the stubbed out comments represents a logical aspect of the application.
  • Now step through each section and implement the simplest possible example highlighting programming concepts along the way.

activity_picker.py

# import the magic random picker

# list of things to pick from
activities_to_do = []

# find a way to pick something

# a way to tell the user what to do

In programming the = is not treated as equality like we are trained for in math, instead = means assignment. The mantra when reading something_on_the_left = 'a value' is: something_on_the_left gets 'a value'

  • Start simple with an empty list.
  • Run the program.
  • Getting no output is the big 'it didn't fail' success!

activity_picker.py

# import the magic random picker

# list of things to pick from
activities_to_do = ['a','b','c']

# find a way to pick something

# a way to tell the user what to do
  • Add a few items to the list.
  • Next print the list and enjoy some output.

activity_picker.py

# import the magic random picker

# list of things to pick from
activities_to_do = ['a','b','c']

# find a way to pick something

# a way to tell the user what to do
print activities_to_do
  • Plugging in the final piece of the equation.
  • Assign going_to_do an element from the list.

activity_picker.py

# import the magic random picker

# list of things to pick from
activities_to_do = ['a','b','c']

# find a way to pick something
going_to_do = activities_to_do[2]

# a way to tell the user what to do
print "The things we can do: " + str(activities_to_do)
print "The thing we are going to do: " + going_to_do
  • At this point the problem is multiple runs of the program yield activity 'c'.
  • To spice things up use random to pick any activity.

activity_picker.py

# import the magic random picker
import random

# list of things to pick from
activities_to_do = ['a','b','c']

# find a way to pick something
going_to_do = random.choice(activities_to_do)

# a way to tell the user what to do
print "The things we can do: " + str(activities_to_do)
print "The thing we are going to do: " + going_to_do
  • Ok buckle up!
  • Reference link
  • Next up,
    • import urllib2
    • GET request to copy the list from github raw.
    • str.split() to convert string of activities into a list.
  • Navigate back to github and click on the 'code_activities_we_like.lst' file.
  • Click on the 'raw' button.
  • Copy the url out of the address bar.
  • Paste the url into a new string and pass it to the library.

activity_picker.py

# import the magic random picker
import random, urllib2

# list of things to pick from
code_activities_we_like = 'https://raw.githubusercontent.com/bellcodo/potential-octo-chainsaw/15142655a8f4faa26d381ca00458fa4fcd8c2423/code_activities_we_like.lst'
activities_to_do = urllib2.urlopen(code_activities_we_like).read()

# find a way to pick something
going_to_do = random.choice(activities_to_do)

# a way to tell the user what to do
print "The things we can do: " + str(activities_to_do)
print "The thing we are going to do: " + going_to_do
  • Run it.
  • Try and understand what the print "The thing we are going to do:" + activities_to_do returned.
  • Now split the result and assign it back to activities_to_do.

activity_picker.py

# import the magic random picker
import random, urllib2

# list of things to pick from
code_activities_we_like = 'https://raw.githubusercontent.com/bellcodo/potential-octo-chainsaw/15142655a8f4faa26d381ca00458fa4fcd8c2423/code_activities_we_like.lst'
activities_to_do = urllib2.urlopen(code_activities_we_like).read()
activities_to_do = activities_to_do.split()

# find a way to pick something
going_to_do = random.choice(activities_to_do)

# a way to tell the user what to do
print "The things we can do: " + str(activities_to_do)
print "The thing we are going to do: " + going_to_do
  • Running the application multiple times should result in different activities.
  • Give it a try to decide what to do next.

hello-dojo-universe

activity-picker

  • activity_picker module using random and urllib2.

building-a-webapp

  • Building a webapp! Using heroku and deploy pipeline.

supreme-system

laughing-meme

refactored-octo-robot

ideal-octo-spoon

  • what it means to exist virtually --profile in webapp

Clone this wiki locally