FAQ: Variables - Introduction to Variables

What it means is not so important as what it represents. The programmer has to make the connection. It is common to create static data within a program to be able to look up user inputs.

The following example is a bit ahead of the curve but you will be exposed to it fairly soon. We have three terms defined in our code,

trees
grass
rocks

We can put all these into a single table and describe what they each refer to…

terrain = {
    'trees': 'trees.jpg',
    'grass': 'grass.jpg',
    'rocks': 'rocks.jpg'
}

When a user inputs a value, we can look up the input in this table. If we get a match, the corresponding image is returned.

user = input('enter a terrain: ')
if user in terrain:
    # attach the image
    image = terrain[user]

Ignore the code, just try to follow what it is suggesting.

1 Like

amazing thanks so much I figured it was a bit ahead of where i was up to but i was just curious.

1 Like