Hello

In my program i have a matrix the code for which is

test = [ ["x" for x in range(5)] for y in range (5) ]
for y in test:
    print " ".join(map(str,y))

which prints something like:

x x x x x
x x x x x
x x x x x
x x x x x
x x x x x
in the program the user inserts numbers in a column with the first input at the bottom and then the next above it and so on.

There are two conditions on which i would like something to be printed.

The first is when a user tries to insert a number in a column that is already full of numbers

eg. when the user tries to insert a number in the first column of the following matrix

1 x x x x
2 x x x x
3 x x x x
4 x x x x
5 x x x x


my current code is

if not x[value][0] == "x":
            print "column full"

where 'value' equals the user inputed column.

My second condition is where all the columns have numbers in them.
My code for this one isn't really working at all.

Thanks

Recommended Answers

All 2 Replies

Why don't use simply:

test = [ ["x" for x in range(5)] for y in range (5) ]
for y in test:
    print " ".join(y)

Which can also be written as:

test = [ ["x" for x in range(5)] for y in range (5) ]
print "\n".join([" ".join(y) for y in test])

My second condition is where all the columns have numbers in them.

This would simply be an extension of your previous code.

def columns_full(x):
    """ Returns True if all columns are full
        Returns False if there are available spaces
    """
    for col in range(5):
        if x[col][0] == "x":     ## Not full
            return False
    return True

Note that you can also store by column and not by row. Using a dictionary to do this, the key = column number, pointing to a list that contains the items in that column.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.