((maybe delete this lessone??))

Use dictionary in CodeCraft

Use dictionary to store (x,y) coordinates in CodeCraft

xh = {1:3, 2:6, 3:9}    # x:h pairs
for k in xh:
    column(key, -20, xh[k], 2)

The above app will build some cobblestone columns at x: 1,2,3 with corresponding height h: 3,6,9.

For your understanding:

Continue with the above app, see if you understand the following code:

for k in xh:
  for i in range(key):
    block(i, xh[k], -20, 'brick')

Did you get the same horizontal brick lines with length of 1,2,3 at vertical locations 3,6,9? See the picture.

If you call on function column() instead of block() in the same nesting loops, what do you expect? Guess before check out the following code and picture.

for key in xy:
  for i in range(key):
    column(i, (-20-i), xy[key], 'brick')

We vary the z value a little so all the structures will show up at different z layers. See in pic??, we get 3 sheets of blocks, width x, height y.

===end here?

Add new key:value pairs or change key's value in a dictionary

Let's make a dictionary my_columns to store material:height data. We will build a set of columns looping through this dictionary my_columns

my_columns = {'brick': 3, 'diamond': 5, 'cobblestone': 8, 'quartz': 12}

# loop through dictionary and build the columns
def build_my_columns(z):
x = 0
for k in my_columns:
  print(k)
  column( x , z, my_columns[k], k)
  x += 2

build_my_columns(-20)

# change key's value (assign new value to a key)
my_columns['diamond'] = 10
my_columns['quartz'] = 3
build_my_columns(-22) # compare and see the different height of diamond and quartz columns

# add new key:value pairs
my_columns['obsidian'] = 15
my_columns['dirt'] = 5
build_my_columns(-25) # walk around and see the last row of columns, new columns were added

my_columns.pop('quartz')
build_my_columns(-18) # see the white quartz column is away from the group

results matching ""

    No results matching ""