This note is from 1 month ago. You're viewing content from a previous lesson.
Today we will learn about procedural generation. You will look at one file. Then you will edit the file to make it unique to your interests:
https://github.com/bmackenty/studentPythonStuff/blob/master/proc_gen_weapon_names.py
Today you will modify a Python program that generates fictional weapons and their stories. You can theme your version around space, animals, mythology, a game, a book, or another interest.
Procedural generation means creating content by following programmed rules. This program randomly selects words and phrases, then combines them into a description.
You do not need to understand every line today. Concentrate on the sections named in each task.
1. Open and run the original program — 10 minutes
my_generator.py.Answer these questions:
Checkpoint: Show your teacher or partner a generated description before you start editing.
2. Read two lines and make your first change — 10 minutes
Find these lines near the top:
weapon_age = ["Peace", "War", "Tranquility", "Sundering", "Rage", "Destruction", "Terror", "Doom", "Despair", "Glory", "Victory", "Darkeness", "Light", "Discovery"]
selected_weapon_age = random.choice(weapon_age)
The first line stores possible age names in a list.
The second line randomly chooses one item from that list and stores it in selected_weapon_age.
Complete this sentence:
random.choice(weapon_age)chooses ____________________.
Now:
"Peace" in the list."Space Exploration".Your new phrase might not appear immediately because the program makes a random choice.
To test your change directly, temporarily replace the whole list line with:
weapon_age = ["Space Exploration"]
Run it again. With only one item available, the program must select that item.
Checkpoint: Find “the age of Space Exploration” in your output.
3. Choose a theme and change two lists — 15 minutes
Choose one theme. If you cannot decide, use space exploration.
Write:
My generator’s theme is ____________________.
First, edit weapon_age.
Replace its contents with at least four age names that fit your theme.
For example:
weapon_age = ["First Contact", "Lost Planets", "Robot Rebellion", "Star Travel"]
Save and run.
Next, find weapon_types_magic.
Replace its contents with at least three items that fit your theme:
weapon_types_magic = ["plasma wand", "gravity staff", "starlight sceptre"]
Save and run.
The program will only use this list in its description when it chooses the "magic" category. To check your new items, find the weapon_category list near the top and temporarily change it to:
weapon_category = ["magic"]
Run the program twice and check the item names.
You may keep this setting if you want your generator to produce only magic items.
Checkpoint: You have changed two lists, and the program still runs.
4. Share a result — 5 minutes
Show a partner one generated description.
Explain:
I changed __________. That affected __________ in the output.
5. Change the materials and history — 15 minutes
First, find weapon_material.
Replace the materials with at least five different materials that fit your theme.
For example:
weapon_material = ["moon rock", "titanium", "star crystal", "meteorite iron", "alien glass"]
Keep at least five materials because this program sometimes selects five at once.
Save and run.
Next, find weapon_past_event_1.
This section contains numbered historical events. Find the entry beginning with 2:.
Replace that entry with your own sentence. For example:
2: "This weapon spent 100 years in an abandoned space station before a robot discovered it.",
Keep:
2:Save and run.
Your event may not appear on every run because the program chooses randomly from several events.
Checkpoint: Your file contains personalized materials and one original historical event.
6. Test and improve your generator — 15 minutes
Show your partner:
Ask your partner:
Which part of my theme can you recognize in the output?
If your program stops working
Check the last line you edited:
| Check | Correct example |
|---|---|
| Text has quotation marks on both sides. | "moon rock" |
| List items have commas between them. | ["moon rock", "alien glass"] |
| The list has opening and closing square brackets. | ["moon rock"] |
| The variable name has not changed. | weapon_material = ... |
If you are still stuck, undo your last edit and run the program again. Ask for help before making more changes.
7. Submit and reflect — 10 minutes
Submit:
my_generator.py file.random.choice() does.Finished early?
Choose one:
human_names and replace three names with fictional character names.