Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Joseph Raker
PLUS
Joseph Raker
Courses Plus Student 3,354 Points

Expanding on Dungeon Game! Returning Mult-Line Lists

Hello! I am working on improving the Dungeon Game as practice. One of my first improvements is to have the player choose the size of the grid. The problem I am running into is that the grid is defined by list that was originally "hand drawn". I have gotten to the code shown below that gives me either a list of lists of tuples or I can combine them into one list of tuples. The problem is, I can't figure out how to get the outputted list to break into multiple lines. Any suggestions would be helpful. Thank you!

nums = 5 #this will be the value entered by the player for the grid size for num in range(nums): l = [] count = nums while count > 0: l.append(list(zip(list(range(nums)), [nums-count]*nums))) count -= 1 print(l) # this gives me the list of lists of tuples l2 = sum(l, []) print(l2) # this gives me the list of tuples (getting closer!)

Steven Parker
Steven Parker
229,608 Points

Use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:
Or watch this video on code formatting.

1 Answer

Steven Parker
Steven Parker
229,608 Points

I'm not entirely sure I understand what you're after, but I'll take a guess — is this it?

for row in l:
      print(row)
Joseph Raker
Joseph Raker
Courses Plus Student 3,354 Points

Thank you for the reply. I apologize for not using markdown. In short, I am trying to make a custom grid that looks like the following (which was entered by hand):

CELLS = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0),
         (0, 1), (1, 1), (2, 1), (3, 1), (4, 1),
         (0, 2), (1, 2), (2, 2), (3, 2), (4, 2),
         (0, 3), (1, 3), (2, 3), (3, 3), (4, 3),
         (0, 4), (1, 4), (2, 4), (3, 4), (4, 4)]

I am using the following code to take an inputted integer value (nums) to define the size of the grid. The problem is, that I need it to look like the list above, so that when the grid gets drawn, it will have the square shape:

def grid_size(nums):
    for num in range(nums):# converts integer input to the correct number of coordinates
        l = []
        count = nums
        while count > 0:
            l.append(list(zip(list(range(nums)), [nums-count]*nums)))
            count -= 1
    print(l) # this gives me the list of lists of tuples
    print("\n")
    for row in l:
        print(row)# this was your suggestion, but what I get is 5 separate lists
grid_size(5)
Steven Parker
Steven Parker
229,608 Points

But you were creating a list of lists, so that's what was displayed.

So what you really want is just one single list, but displayed as an assigment in rows:

def grid_size(nums):
    grid = []
    for count in range(nums):
        grid.extend(zip(range(nums), [count]*nums))  # make just ONE list

    # display in rows as an assignment to CELLS
    for cell in grid:
        print("CELLS = [" if cell==(0,0) else " "*9 if cell[0]==0 else " ", end="")
        print(cell, end="," if cell[0]<nums-1 else ",\n" if cell[1]<nums-1 else "]\n")

You could easily return the grid if you wanted to use it in the program instead of (or in addition to) printing it.

Joseph Raker
Joseph Raker
Courses Plus Student 3,354 Points

Thank you Steven. You have been very helpful. My initial error was in thinking that the multiline list was necessary for the grid, when in fact the map drawing portion of the script set the shape. I was able to use your help to update the script to accept user input on the grid size and produce the correct image and playability. Thank you again.