Full-Stack Development Hackbright Part-Time Classes

Ready, Set, Code!

Week 1

Coding Kata

We'll be coding up our own web server next week, so let's make sure our skills are sharp!

Choose your own Adventure

In building our web server and web apps, we'll need the following programming concepts. We will be building on them as we go along, so don't feel you have to know //everything// right now.

  • Variables
  • Lists and Dictionaries
  • For and while loops
  • If, else, and testing if things are true
  • Functions and return values
  • Reading from a file
  • How to run a program from the command line

New to Programming:

Check out the links in the extra resources section. Spend a little time on Codecademy getting familiar with the syntax, and then move on to Learn Python the Hard Way.

If you want a more guided tour, this older course material might be helpful.

Experienced Programmer but new to Python:

Try jumping straight into the exercises below. Look up things as you need to - the links in the extra resources section should help point you in the right direction.

Experienced with Python:

Run through the exercises below, and the assignment.

If you get done with that, there are a couple more things for you to do as programming practice in the extra resources section. If you want to move on and get a headstart, see if you can figure out this web challenge.

Exercise: Superhero Names (Lists and Files)

First, download the hero and villain names file. This should be a file ending in '.txt'. Move it to your Python working directory.

Part 1

Write a Python script which loads the names from the file, and converts your own name to a superhero (or supervillain) name by picking the name corresponding to your first name's position in the alphabet (there are 26 superheroes in the list). For example, if your name starts with A, you want superhero name #1. Mine (J) is name #10.

Here's a snippet of code to help with the letter part of this:

import string
uppers = string.uppercase
print uppers  # this will help you figure out what this is
j_position = uppers.find('J')  # what do you think .find does?

(By the way, I want to point out that Princess Python is a real character.)

Part 2

Use the random.choice and split() functions to pick a randomly-selected first name and second name.

import random

beatles = ["John", "Paul", "George", "Ringo"]
# Print the name of a random Beatle
print random.choice(beatles)
Part 3

Expand the superhero generator to avoid names where the first and last name are the same, such as 'Poison Poison'.

Exercise: BARRRtender

To get really down and dirty with functions and dictionaries, let's create a new app which specializes in bartending. Pirate barrrrrtending.

The bartender will invent a new and delicious cocktail for you based upon your answers to some simple questions.

Create questions and ingredient dictionaries

The bartender should ask questions that determine your tastes and then identify ingredients to suit those tastes. If you like you can use the example bartender below, but feel free to customize!

questions = {
    "strong": "Do ye like yer drinks strong?",
    "salty": "Do ye like it with a salty tang?",
    "bitter": "Are ye a lubber who likes it bitter?",
    "sweet": "Would ye like a bit of sweetness with yer poison?",
    "fruity": "Are ye one for a fruity finish?"
}

ingredients = {
    "strong": ["glug of rum", "slug of whisky", "splash of gin"],
    "salty": ["olive on a stick", "salt-dusted rim", "rasher of bacon"],
    "bitter": ["shake of bitters", "splash of tonic", "twist of lemon peel"],
    "sweet": ["sugar cube", "spoonful of honey", "spash of cola"],
    "fruity": ["slice of orange", "dash of cassis", "cherry on top"]
}
Write a function to ask what style of drink a customer likes.
  • The function should ask each of the questions in the questions dictionary, and gather the responses in a new dictionary.
  • The new dictionary should contain the type of ingredient (for example "salty", or "sweet"), mapped to a Boolean (True or False) value.
  • If the customer answers y or yes to the question then the value should be True, otherwise the value should be False.
  • The function should return the new dictionary.

You can use the raw_input function to get an answer from the landlubber... er, customer. This exercise is a good introduction (or reminder).

Write a function to construct a drink
  • The function should take the preferences dictionary created in the first function as a parameter.
  • Inside the function you should create an empty list to represent the drink.
  • For each type of ingredient which the customer said they liked you should append a corresponding ingredient from the ingredients dictionary to the drink.
  • Finally the function should return the drink.

To choose an ingredient from one of the ingredient lists you can use the random.choice function again.

Provide a main function

Use if __name__ == '__main__': to run this function from the command line. The main() function should call your two functions in order, passing your list of preferences to the drink creation function. It should then print out the contents of the drink.

# code
# code
# code
def main():
    # first piece of code you want to run in this file
    print "main"
    

if __name__=="__main__":
    main() 
# this means "if this is run from the command line, run the main() function"

If this is getting odd results make sure that all your other code is inside functions so you don't end up accidentally running it when you run the file.

To run the file, it's just python bartender.py (or whatever you called it).

Discussion

Once you've completed the basic requirements for the project, feel free to take a look at this sample solution. Compare and contrast your solution. What do you like better about the sample? What do you like better about yours?

Improve your Jedi Skills

If you complete both these exercises, or just want more practice, try Code Kata at Cyber Dojo.

Hit "create", select the "Python" language and then pick a problem. Hit "enter" to start the kata. Your goal is to create a function inside the test file that tests for, and solves, the problem. This blog post breaks down the process in more detail.