Skip to content
Advertisement

print gives different output

I’m trying to make a 12×12 binary-puzzle game in python. There’s no GUI in the game, just the shell. To get the values of the tiles, I made an import function which imports values from an excel-document using xlrd. Now I’m making a function to print out the board, but something’s not quite right:

enter image description hereenter image description here

(sorry for too big pictures)

Here is my code:

Main:

import Import as imp
import print as prt

data = imp.getInput("C:/Users/jaspe/PyCharmProjects/BinaryPuzzle/INPUT.xls")
print(data)
print(data[6])
prt.printField(data)

Import:

import xlrd


def getInput(loc):
    wb = xlrd.open_workbook(loc)
    sheet = wb.sheet_by_index(0)
    data = [[], [], [], [], [], [], [], [], [], [], [], []]
    i = 0
    while i < 12:
        data[i].append(sheet.cell_value(i, 0))
        data[i].append(sheet.cell_value(i, 1))
        data[i].append(sheet.cell_value(i, 2))
        data[i].append(sheet.cell_value(i, 3))
        data[i].append(sheet.cell_value(i, 4))
        data[i].append(sheet.cell_value(i, 5))
        data[i].append(sheet.cell_value(i, 6))
        data[i].append(sheet.cell_value(i, 7))
        data[i].append(sheet.cell_value(i, 8))
        data[i].append(sheet.cell_value(i, 9))
        data[i].append(sheet.cell_value(i, 10))
        data[i].append(sheet.cell_value(i, 11))
        j = 0
        while j < len(data[i]):
            if data[i][j] != "":
                data[i][j] = int(data[i][j])
            j = j + 1
        i = i + 1
    return data

Print:

def printField(d):
    i = 0
    d_ = []
    while i < len(d):
        d_.append([])
        j = 0
        while j < len(d[i]):
            d_[i].append(d[i][j])
            if d_[i][j] == "":
                d_[i][j] = "_"
            j = j + 1
        i = i + 1
    space = "  "
    print(str(d_[0][0]) + space + str(d_[0][1]) + space + str(d_[0][2]) + space + str(d_[0][3]) + space + str(
        d_[0][4]) + space + str(d_[0][5]) + space + str(d_[0][
                                                          6]) + space + str(d_[0][7]) + space + str(
        d_[0][8]) + space + str(d_[0][8]) + space + str(d_[0][9]) + space + str(d_[0][10]) + space + str(d_[0][11]))
    print(str(d_[1][0]) + space + str(d_[1][1]) + space + str(d_[1][2]) + space + str(d_[1][3]) + space + str(
        d_[1][4]) + space + str(d_[1][5]) + space + str(d_[1][
                                                          6]) + space + str(d_[1][7]) + space + str(
        d_[1][8]) + space + str(d_[1][8]) + space + str(d_[1][9]) + space + str(d_[1][10]) + space + str(d_[1][11]))
    print(str(d_[2][0]) + space + str(d_[2][1]) + space + str(d_[2][2]) + space + str(d_[2][3]) + space + str(
        d_[2][4]) + space + str(d_[2][5]) + space + str(d_[2][
                                                          6]) + space + str(d_[2][7]) + space + str(
        d_[2][8]) + space + str(d_[2][8]) + space + str(d_[2][9]) + space + str(d_[2][10]) + space + str(d_[2][11]))
    print(str(d_[3][0]) + space + str(d_[3][1]) + space + str(d_[3][2]) + space + str(d_[3][3]) + space + str(
        d_[3][4]) + space + str(d_[3][5]) + space + str(d_[3][
                                                          6]) + space + str(d_[3][7]) + space + str(
        d_[3][8]) + space + str(d_[3][8]) + space + str(d_[3][9]) + space + str(d_[3][10]) + space + str(d_[3][11]))
    print(str(d_[4][0]) + space + str(d_[4][1]) + space + str(d_[4][2]) + space + str(d_[4][3]) + space + str(
        d_[4][4]) + space + str(d_[4][5]) + space + str(d_[4][
                                                          6]) + space + str(d_[4][7]) + space + str(
        d_[4][8]) + space + str(d_[4][8]) + space + str(d_[4][9]) + space + str(d_[4][10]) + space + str(d_[4][11]))
    print(str(d_[5][0]) + space + str(d_[5][1]) + space + str(d_[5][2]) + space + str(d_[5][3]) + space + str(
        d_[5][4]) + space + str(d_[5][5]) + space + str(d_[5][
                                                          6]) + space + str(d_[5][7]) + space + str(
        d_[5][8]) + space + str(d_[5][8]) + space + str(d_[5][9]) + space + str(d_[5][10]) + space + str(d_[5][11]))
    print(str(d_[6][0]) + space + str(d_[6][1]) + space + str(d_[6][2]) + space + str(d_[6][3]) + space + str(
        d_[6][4]) + space + str(d_[6][5]) + space + str(d_[6][
                                                          6]) + space + str(d_[6][7]) + space + str(
        d_[6][8]) + space + str(d_[6][8]) + space + str(d_[6][9]) + space + str(d_[6][10]) + space + str(d_[6][11]))
    print(str(d_[7][0]) + space + str(d_[7][1]) + space + str(d_[7][2]) + space + str(d_[7][3]) + space + str(
        d_[7][4]) + space + str(d_[7][5]) + space + str(d_[7][
                                                          6]) + space + str(d_[7][7]) + space + str(
        d_[7][8]) + space + str(d_[7][8]) + space + str(d_[7][9]) + space + str(d_[7][10]) + space + str(d_[7][11]))
    print(str(d_[8][0]) + space + str(d_[8][1]) + space + str(d_[8][2]) + space + str(d_[8][3]) + space + str(
        d_[8][4]) + space + str(d_[8][5]) + space + str(d_[8][
                                                          6]) + space + str(d_[8][7]) + space + str(
        d_[8][8]) + space + str(d_[8][8]) + space + str(d_[8][9]) + space + str(d_[8][10]) + space + str(d_[8][11]))
    print(str(d_[9][0]) + space + str(d_[9][1]) + space + str(d_[9][2]) + space + str(d_[9][3]) + space + str(
        d_[9][4]) + space + str(d_[9][5]) + space + str(d_[9][
                                                          6]) + space + str(d_[9][7]) + space + str(
        d_[9][8]) + space + str(d_[9][8]) + space + str(d_[9][9]) + space + str(d_[9][10]) + space + str(d_[9][11]))
    print(str(d_[10][0]) + space + str(d_[10][1]) + space + str(d_[10][2]) + space + str(d_[10][3]) + space + str(
        d_[10][4]) + space + str(d_[10][5]) + space + str(d_[10][
                                                            6]) + space + str(d_[10][7]) + space + str(
        d_[10][8]) + space + str(d_[10][8]) + space + str(d_[10][9]) + space + str(d_[10][10]) + space + str(d_[10][11]))
    print(str(d_[11][0]) + space + str(d_[11][1]) + space + str(d_[11][2]) + space + str(d_[11][3]) + space + str(
        d_[11][4]) + space + str(d_[11][5]) + space + str(d_[11][
                                                            6]) + space + str(d_[11][7]) + space + str(
        d_[11][8]) + space + str(d_[11][8]) + space + str(d_[11][9]) + space + str(d_[11][10]) + space + str(d_[11][11]))

I know it’s some inefficient code but its not stupid if it works(but it doesn’t work so it’s stupid)

tnx 4 helping!

EDIT: Tnx to @blorgon and @T1Berger, the issue is fixed, tnx guys!

Advertisement

Answer

You got a copy and paste error in every index space with str(d_[x][8])

As @blorgon pointed out you should refactor your code – with for the example a foreach loop going through the elements of your nested array. A simple list comprehension should do the trick and is easier to code and to understand:

[print(' '.join(nested_list )) for nested_list in data]
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement