0

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

Edited by G-nom: n/a

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by woooee
0

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])

Edited by pyTony: n/a

0

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.

Edited by woooee: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.