Category Archives: Functional Programming in Python

Week 8 – 3/21/15

Next class we are going to be building structures in Minecraft. The code would look like something like this:
mc.setBlock(x, y, z, block_type)
So for example:
mc.setBlock(100, 200, 300, 3)
would mean to set a block at position:
z =300
block_type =3 —> dirt
I’d like you to write some code to print out numbers. In class I will show you how we can modify this code with very little extra code and build walls. It’s all about “for” loops.
Do a search on Google for “python for loop and nested for loops” if you want a refresher. There are tons of good videos. It’s a great habit to use Google to help you learn. I use it all the time.
A. write a program that will print out the following using a for loop:
B. Write a program that will print out the following using a nested for loop:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Week 6 – 3/7/2015

In class we went over how to write a function for our hangman game that will take a word as an input and then return a string with the correct number of underscores. For example
“banana”   —->  “_ _ _ _ _ _ _ _”
Now write a function that takes 3 inputs. First_Name, Middle_Name, and Last Name and returns a string which is the initials.
Lets call the function “Initials”
 Initials(“Michael”, “Zvi”, “Liebis”) —–> “MZL”
Some help:
if you have a string
x = “Michael”
x[0] is the first character or “M”
Test out this code:
def sum(x,y):
return x+y
s = sum(2,3)
print s

Week 4 – Cat in the Hat

I made this for my daughter, Navah,  and thought some of my students might have some fun ideas as to how to make it better. If you modify the code I will show it to her.
I also need to make one for “AT” words. CAT, HAT, SAT etc. So feel free to make it and share. Thinking might be able to do a fun cat in the hat version.
Hope everyone is enjoying the break,

Week 4 – 1/31/15 – Hangman Part 1

Hang Man
We are going to code up the game Hangman  in Python. Once we do this we can then use Minecraft as a graphical interface for the game. This should be a lot of fun.

In order to do this we are going to be manipulating Strings. I am going to give you simple excercises that will help you code up the game. As always write a few lines of code and test.

A:   Lets assume that the mystery_word is “minecraft” so we will want to display  “ _ _ _ _ _ _ _ _”

Type in the following in IDLE

x= “minecraft”

What did this return?

y = “ab”
z = y + y

What did this return?

Now can you write a script where you set x equal to a word and we get the correct display?

For example:

x=“hello”  creates   “ _ _ _ _ _”

We might want to use this output again so how do you store it? What sort of variable do we use?

B:  Lets assume that the mystery_word is “chicken” and then the user is asked to guess a letter. Assume the user guesses “c”.  We then need to know that the the 1st and 4th letters have a c.

Python has many tools to help you with Strings. So there are many ways to do things and feel free to get creative.

Type in:

x= “minecraft”

What did this return?

Now type:

y = “hello”

What did this return?

Now type:

temp = y[0]
temp y[1]

Now write a script that takes a word and prints out a letter on each line so:

x = “summer”

prints out:


Now lets go back a bit:

y = “hello”
temp = y[0]

What printed out?

temp = y[1]
what printed out?
temp = y[2]

What printed out?

When we are repeating lines of code what is a good idea?

Now try to write a script that finds where a letter appears in a word (remember first position is 0)

So if:

x = “hello”
guess = “l”

we want to get 2,3.

Feel free to email me with questions or post them right here.

Week 3 – 1/24/15

There is an easy way to do this assignment. I am going to ask you to do it that way first. Then I want you to do it in a different way. It will become clear once we learn about functions why I want you to do it the second way too.
1. Ask the user for 5 numbers and have the program print out the largest number.
2. Ask the user for 5 numbers and and only after all 5 numbers are stored in memory do I want you to find the largest number. By stored in memory I mean that they are in buckets. You can use 5 variables or a list. Either way is good as we can then go over the advantage/disadvantage of each method.

Week 2 – 1/17/15

We talked a bit about variables in Python. We used the syntax:

"variable_name" = value

For example:

 x = 5

We talked about how you can think about this as there being a bucket that you place 5 in and then when you want to know what is in the bucket you use the variable x.

Now think about the snippet of code below run it in Python. Test it!!!

x = 5 
y = 7
y = x
print y

What prints out?

Now explain in words what is going on. Explain it with buckets if you’d like.

Now write a program in Python that asks the user for a number between 1-10. Then it asks the user for a second number between 1-10. Then prints out “Same” if the two numbers are the same and “Different” if they are different.

Now add a “for” loop to have the game played 3 times.

Then use a “while” loop to have the game played over and over again where the user is propted after each game to find out if he wants to play again or not.

Programming Minecraft With Raspberry Pi

Some of the girls expressed interest in programming the Minecraft world in Python. The best way to do this is on a Raspberry Pi. You can find some information on the process and hardware requirements here. While there are many options, the Kano Raspberry Pi kit comes in a simple package with everything we’ll need to get started. The kit is only $150 and can be purchased through their website. We will bring our Kano to the next class so you can check it out.

Week 1 – 1/10/15

Last class we  went over a bunch of control structures.

Note that “#” is used for comments in Python. The interpreter skips over the line. I will also be using # to start each script. Please run the following short scripts for a review:

# This is a script to demonstrate for loops
 for x in range(0,10):
# This is a script to demonstrate if statements
 if x < 10:
# This is a script to demonstrate how to generate # a random integer
 from random import randint
 x= randint(0,5)
# This is a script to demonstrate how to get user # input
 x = input("Please input a number: ")
# This is a script to demonstrate how to put it  # all together. Note the "==" to test equality 
 from random import randint
 x= randint(0,5)
 guess = input("Please guess a number from 0-5:") 
 if (guess == x):
    print "You Win"
    print "You Lose"

This weeks assignment is to write a short python script. Have the computer select a random number between 1-20. Then have the played guess the number. If the player guesses corectly the computer prints out “You Win” otherwise it prints out “You Loose”