Created
May 12, 2014 04:16
-
-
Save Isweet/5e80b6eb8e9184d27d54 to your computer and use it in GitHub Desktop.
Everyone Can Code! Tutorial Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import turtle | |
# 1. Hello World! | |
# This is designed as a first introduction to the language. | |
# One print statement and done! | |
def print_hello_world(): | |
print "Hello, World!" | |
# 2. Say My Name, Say My Name (Beyonce, anyone?) | |
# This is designed as a first-stab at user input. | |
# Asks the user what their name is, and says hello to them. | |
def say_hello_back(): | |
print "Hello, %s!" % (raw_input('What is your name? ')) | |
# 3. Valve Say Whaaaaat? (Heh, get it? Because flow control...) | |
# This is designed as a basic introduction to flow control. | |
# It builds on the previous two exercises. | |
def say_something_smart(): | |
age = int(raw_input('How old are you? ')) | |
if (age > 30): | |
print "You are very wise." | |
elif (age < 20): | |
print "You have much to learn, grasshopper." | |
else: | |
print "You are just a hooligan!" | |
# 4. Pop Quiz! | |
# A very simple quiz question generator for arithmetic. | |
# Introduces the idea of the "standard library" by using the random number | |
# generator. This returns boolean so we may use it in the next example. | |
def quiz_me(): | |
a = random.randint(1, 10) | |
b = random.randint(1, 10) | |
answer = int(raw_input('What is %d + %d? ' % (a, b))) | |
if (a + b == answer): | |
print "Correct!" | |
return True | |
else: | |
print "Incorrect." | |
return False | |
# 5. Hoola-Loop | |
# Let's use the functions we just wrote to generate a full-blown quiz. | |
# This introduces the idea of a function parameter, looping, and incrementing | |
# an int. | |
def do_quiz(lim): | |
current_round = 0 | |
num_correct = 0 | |
say_hello_back() | |
say_something_smart() | |
print "I challenge you to an arithmetic duel!" | |
while (current_round < lim): | |
was_correct = quiz_me() | |
if (was_correct): | |
num_correct += 1 | |
current_round += 1 | |
print "You score a %d/%d." % (num_correct, lim) | |
# 6. FizzBuzz Lightyear | |
# This is a basic implementation of the FizzBuzz puzzle. Introduces the | |
# range() function along with the notion of a for-each loop. For now, just | |
# present it as a normal for-loop. White lie. | |
def fizzbuzz(): | |
for current_number in range(1, 101): | |
if current_number % 15 == 0: | |
print "FizzBuzz" | |
elif current_number % 3 == 0: | |
print "Fizz" | |
elif current_number % 5 == 0: | |
print "Buzz" | |
else: | |
print current_number | |
# 7. Listify FizzBuzz | |
# Lists are super powerful in Python, and you can manipulate them a hundred different ways. | |
# That being said, for now we should just stick to basic adding, indexing, etc. This focuses on | |
# creating an empty list and adding to the end. | |
def listify_fizzbuzz(): | |
my_list = [] | |
for current_number in range(1, 101): | |
if current_number % 15 == 0: | |
my_list.append("FizzBuzz") | |
elif current_number % 3 == 0: | |
my_list.append("Fizz") | |
elif current_number % 5 == 0: | |
my_list.append("Buzz") | |
else: | |
my_list.append(current_number) | |
return my_list | |
# 8. FizzQuery | |
# So I know I'm beating this FizzBuzz thing to death, and next time we'll be done, but we need | |
# a little more practice with lists. So now let's focus on indexing into lists. If you didn't address | |
# this in the earlier exercises, you might want to discuss casting. Or you can keep putting it off, either one. | |
# Please note that this will crash if they provide something that is not a number, or if they go out of bounds. | |
# Try-catch is outside the scope of this tutorial. | |
def fizz_query(): | |
fizz_list = listify_fizzbuzz() | |
print "Enter any number between 1 and 100 to get the fizzbuzz value. If you want to quit, type \"quit\"." | |
while (True): | |
query = raw_input('>> ') | |
if (query == "quit"): | |
break | |
else: | |
#notice the - 1, talk about 0-based indexing | |
print fizz_list[int(query) - 1] | |
# 9. Prime Sieve | |
# This is quite a leap in difficulty, but its a fantastic example which combines a lot of the concepts | |
# we've discussed so far. Looping, lists, modulo, if-statements. Visit: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes | |
# for a description of the algorithm. | |
def prime_sieve(lim): | |
is_prime = lim*[True] | |
current_number = 2 | |
while (current_number < lim): | |
multiply_by = 2 | |
current_multiple = current_number * multiply_by | |
while (current_multiple < lim): | |
is_prime[current_multiple] = False | |
multiply_by += 1 | |
current_multiple = current_number * multiply_by | |
current_number += 1 | |
while (current_number < lim and not is_prime[current_number]): | |
current_number += 1 | |
# Here's the kick ass way to go from boolean list -> primes | |
#return [i + 2 for i, elem in enumerate(is_prime[2:]) if elem] | |
return_list = [] | |
for num in range(len(is_prime)): | |
if (num is not 0 and num is not 1 and is_prime[num]): | |
return_list.append(num) | |
return return_list | |
# 10. Recursion and the turtle package | |
# This is some serious extra-credit. It introduces the concepts of recursion, and uses a standard library package | |
# called turtle which makes creating simple animations very very easy. This was stolen shamelessly from: | |
# http://interactivepython.org/runestone/static/pythonds/Recursion/graphical.html | |
def tree(branchLen, current_turtle): | |
if branchLen > 5: | |
current_turtle.forward(branchLen) | |
current_turtle.right(20) | |
tree(branchLen - 15, current_turtle) | |
current_turtle.left(40) | |
tree(branchLen - 15, current_turtle) | |
current_turtle.right(20) | |
current_turtle.backward(branchLen) | |
def animate_tree(): | |
my_turtle = turtle.Turtle() | |
myWin = turtle.Screen() | |
my_turtle.left(90) | |
my_turtle.up() | |
my_turtle.backward(100) | |
my_turtle.down() | |
my_turtle.color("green") | |
#Change the first argument to "tree" in order to make the tree more/less awesome -- hint: 100 is pretty awesome | |
tree(100, my_turtle) | |
myWin.exitonclick() | |
# THE END! | |
# This is all the resources I'm going to include in the "standard beginners guide". | |
# That being said, I have tons more resources, both in the scope of this guide | |
# as well as beyond the scope of any of this (think packet creation and 2d games) | |
# print_hello_world() | |
# say_hello_back() | |
# say_something_smart() | |
# quiz_me() | |
# do_quiz(5) | |
# fizzbuzz() | |
# print listify_fizzbuzz() | |
# fizz_query() | |
# print prime_sieve(50) | |
# animate_tree() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment