In this chapter, we are going to use the Python programming language. Almost all programming languages are capable of doing the same things, but they are usually designed with different specializations. Some languages are designed to perform one job particularly well, some are designed to run code as fast as possible, and some are designed to be easy to learn.
Scratch was designed to develop animations and games, and to be easy to read and learn, but it can be difficult to manage large programs. Python is designed to be a good general-purpose language. It is easy to read and can run code much faster than Scratch.
Python is a text-based language. Using it, we type the code rather than arrange building blocks. This makes it easier to go back and change the pieces of code that we have already written, and it allows us to write complex pieces of code more quickly. It does mean that we need to type our programs accurately, though—there are no limits to what we can type, but not all text...
As mentioned earlier, our program is going to generate random, possibly funny, phrases for us. To do this, we're going to give each phrase a common structure, and randomize the word that appears in each position. Each phrase will look like:
<name> has a <adjective> <noun>
Where <name>
is replaced by a person's name, <adjective>
is replaced by a descriptive word, and <noun>
is replaced by the name of an object.
This program is going to be a little larger than our previous code example, so we're going to want to save it and modify it easily. Navigate to File | New Window in Python 2. A second window will appear which starts off completely blank. We will write our code in this window, and when we run it, the results will appear in the first window. For the rest of the chapter, I will call the first window the Shell, and the new window the Code Editor. Remember to save your code regularly!
So, we have random phrases being displayed, but what if we now want to make them less random? What if you want to show your program to a friend, but make sure that it only ever says nice things about you, or bad things about them? In this section, we'll extend the program to do just that.
The first thing we're going to do is replace one of our lists with a dictionary. A dictionary in Python uses one piece of information (a number, some text, or almost anything else) to search for another. This is a lot like the dictionaries you might be used to, where you use a word to search for its meaning. In Python, we say that we use a key to look for a value.
We're going to turn our adjectives
list into a dictionary. The keys will be the existing descriptive words, and the values will be tags that tell us what sort of descriptive words they are. Each adjective will be "good"
or "bad"
.
My adjectives
list becomes the following dictionary. Make similar changes to yours.
adjectives...
Here is the final code you should have when you have completed this chapter. You can use this code listing to check that you have everything in the right order, or look for other problems in your code. Of course, you are free to change the contents of the lists and dictionaries to whatever you like; this is only an example:
import random def chooseAdjective(tag): while True: item = random.choice(adjectives.keys()) if adjectives[item] == tag: break return item names = ["Alice", "Bob", "Carol", "Dave"] adjectives = {"fast":"good", "slow":"bad", "pretty":"good", "smelly":"bad"} nouns = ["dog", "car", "face", "foot"] name = random.choice(names) #adjective = random.choice(adjectives) noun = random.choice(nouns) if name == "Alice": adjective = chooseAdjective("good") elif name == "Bob": adjective = chooseAdjective("bad") else: adjective = random.choice(adjectives.keys()) print name, "has a", adjective, noun
In this chapter, we learned about the Python programming language and how it can be used to create random phrases. We saw that it shared lots of features with Scratch, but is simply presented differently.
In the next chapter, we'll continue learning about Python, and will discover how to allow computer code to interact with the physical world.