Procedural generation to make a galaxy.

Search for life

Try

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

Giving a list some data when it is declared is called enumerating. In this program, the lists creature, colour and characteristic are all enumerated with data for the program to use.

As the size of the data structures for creature, colour and characteristic never change when the program is running, we can think of these structures as arrays.

Arrays are static and do not change their size. Lists are dynamic and do change their size.

Investigate

Questions to think about with this program to check your understanding:

Purpose question

What is the purpose using the variable planet in random.seed(planet) in line 17?

Reveal answer

Every time you run the program you need the planet to be the same as it was last time the program was run. By setting the seed using the planet variable you can ensure the same deterministic set of values are always generated for a given planet by the (not so) random number generator.

This is a pretty neat trick called procedural generation used in games development. Instead of storing all the data, the computer can generate it when it needs it instead.

In this example, we can have billions of planets that stay the same while only using a small fraction of memory because the attributes are stored, not the planets.

Reason question

Why are the random indexes in lines 18-20 between 0 and 2 instead of between 1 and 3 given that creature, colour and characteristic can have one of three possible attributes?

Reveal answer

Lists are zero-indexed. I.e. they start at zero and not one. E.g. creature[0] is “lizards”, creature[1] is “humanoids” and creature[2] is “insects”.

Make

Change the program so that:

  1. It allows for planets to have a hot, frozen, barren or a temperate climate.
  2. The weather on the planet can be one of four values corresponding to the climate.
  3. Only temperate planets can sustain life.
  4. The output messages match those shown below.

Typical inputs and outputs from the program would be:

Enter the catalogue number of a planet: 10
Probes report a hot planet with no signs of life.
Enter the catalogue number of a planet: 11
Probes report green, angry insects on a temperate planet.
Enter the catalogue number of a planet: 12
Probes report blue, docile humanoids on a temperate planet.
Enter the catalogue number of a planet: 13
Probes report a barren planet with no signs of life.
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
# Search for life program

# -------------------------
# Import libraries
# -------------------------
import random


# -------------------------
# Subprograms
# -------------------------
# Function to return what probes find
def probe(planet):
---
    # Ensure the planet always returns the same result 
    random.seed(planet)
---
    
    climate = ["hot", "frozen", "barren", "temperate"]
    weather = random.randint(0,3)
---
    
    # Only temperate planets can host life
    if weather == 3:
---
        creature = ["lizards", "humanoids", "insects"]
        colour = ["red", "green", "blue"]
        characteristic = ["shy", "angry", "docile"]
        lifeform = random.randint(0,2)
        specimen = random.randint(0,2)
        behaviour = random.randint(0,2)
---
        report = ""
        report = colour[specimen]
---
        report = report + ", " + characteristic[behaviour]
---
        report = report + " " + creature[lifeform]
---
        report = report + " on a " + climate[weather] + " planet."
---
    else:
---
        report =  "a " + climate[weather] + " planet with no signs of life."
---
    return report
---
    
    
# -------------------------
# Main program
# -------------------------
---
planet = int(input("Enter the catalogue number of a planet: "))
---
report = probe(planet)
---
print("Probes report", report)