Home > Back-end >  Print specific values from a dictionary, then go to the next line
Print specific values from a dictionary, then go to the next line

Time:09-11

Right now I am manually typing in every single key for the print lines at the bottom like this :

grid = {}
coord = {}
gridRender = {}

yCoord = -10

for xGrid in range(-5, 6):
    for yGrid in range(-5, 6):
        gridRender[xGrid, yGrid] = "░░"
        gridRender[0, yGrid] = "▒▒"
        gridRender[xGrid, 0] = "▒▒"

for xCoord in range(-5, 6):
    try:
        yCoord = round(sin(xCoord))
    except:
        yCoord = -10
    gridRender[xCoord, yCoord] = "██"

print(gridRender[-5, 5]   gridRender[-4, 5]   gridRender[-3, 5]   gridRender[-2, 5]   gridRender[-1, 5]   gridRender[0, 5]   gridRender[1, 5]   gridRender[2, 5]   gridRender[3, 5]   gridRender[4, 5]   gridRender[5, 5])
print(gridRender[-5, 4]   gridRender[-4, 4]   gridRender[-3, 4]   gridRender[-2, 4]   gridRender[-1, 4]   gridRender[0, 4]   gridRender[1, 4]   gridRender[2, 4]   gridRender[3, 4]   gridRender[4, 4]   gridRender[5, 4])
print(gridRender[-5, 3]   gridRender[-4, 3]   gridRender[-3, 3]   gridRender[-2, 3]   gridRender[-1, 3]   gridRender[0, 3]   gridRender[1, 3]   gridRender[2, 3]   gridRender[3, 3]   gridRender[4, 3]   gridRender[5, 3])
print(gridRender[-5, 2]   gridRender[-4, 2]   gridRender[-3, 2]   gridRender[-2, 2]   gridRender[-1, 2]   gridRender[0, 2]   gridRender[1, 2]   gridRender[2, 2]   gridRender[3, 2]   gridRender[4, 2]   gridRender[5, 2])
print(gridRender[-5, 1]   gridRender[-4, 1]   gridRender[-3, 1]   gridRender[-2, 1]   gridRender[-1, 1]   gridRender[0, 1]   gridRender[1, 1]   gridRender[2, 1]   gridRender[3, 1]   gridRender[4, 1]   gridRender[5, 1])
print(gridRender[-5, 0]   gridRender[-4, 0]   gridRender[-3, 0]   gridRender[-2, 0]   gridRender[-1, 0]   gridRender[0, 0]   gridRender[1, 0]   gridRender[2, 0]   gridRender[3, 0]   gridRender[4, 0]   gridRender[5, 0])
print(gridRender[-5, -1]   gridRender[-4, -1]   gridRender[-3, -1]   gridRender[-2, -1]   gridRender[-1, -1]   gridRender[0, -1]   gridRender[1, -1]   gridRender[2, -1]   gridRender[3, -1]   gridRender[4, -1]   gridRender[5, -1])
print(gridRender[-5, -2]   gridRender[-4, -2]   gridRender[-3, -2]   gridRender[-2, -2]   gridRender[-1, -2]   gridRender[0, -2]   gridRender[1, -2]   gridRender[2, -2]   gridRender[3, -2]   gridRender[4, -2]   gridRender[5, -2])
print(gridRender[-5, -3]   gridRender[-4, -3]   gridRender[-3, -3]   gridRender[-2, -3]   gridRender[-1, -3]   gridRender[0, -3]   gridRender[1, -3]   gridRender[2, -3]   gridRender[3, -3]   gridRender[4, -3]   gridRender[5, -3])
print(gridRender[-5, -4]   gridRender[-4, -4]   gridRender[-3, -4]   gridRender[-2, -4]   gridRender[-1, -4]   gridRender[0, -4]   gridRender[1, -4]   gridRender[2, -4]   gridRender[3, -4]   gridRender[4, -4]   gridRender[5, -4])
print(gridRender[-5, -5]   gridRender[-4, -5]   gridRender[-3, -5]   gridRender[-2, -5]   gridRender[-1, -5]   gridRender[0, -5]   gridRender[1, -5]   gridRender[2, -5]   gridRender[3, -5]   gridRender[4, -5]   gridRender[5, -5])

However this seems like an unnecessarily long way of doing this and if I wanted to make the grid any bigger, it would take a while to type in every value. Is there a better way of doing this?

CodePudding user response:

Use a loop and join()

for y_coord in range(5, -6, -1):
    print(''.join(gridRender[x_coord, y_coord] for x_coord in range(-5, 6)))
  • Related