Book Image

Raspberry Pi Projects for Kids (Second Edition)

By : Daniel Leonard Bates
Book Image

Raspberry Pi Projects for Kids (Second Edition)

By: Daniel Leonard Bates

Overview of this book

Table of Contents (14 chapters)

Complete code listing


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