Is there something wrong with this code i wrote? i get the feeling i made a mistake and cant quite recall what, its quite frustrating.
The function is supposed to determine the dimensions of a matrix and if it is, or is not, a valid matrix ( for example, if a row in the matrix isn't equal to another, and such)

def matrixDimensions(m):
	z = []
	ms = len(str(m).split('[')) - 2
	for x in range(ms):
		z.append(len(m[x]))
	if z[0] == z[1]:
		return "This is a %dx%d matrix." % ((len(z)),z[0])
	else:
		return "This is not a valid matrix."

im sure i spotted an error but i cant recall it. It works when i tested it but i think there is an error the tests i used wouldn't catch.
Please help, would me much appreciated.

i think i spotted the error, or at least, an error.
z could be any length, so i am working to change the code, so far i got

def ma(m):
	z = []
	test = []
	ms = len(str(m).split('[')) - 2
	for x in range(ms):
		z.append(len(m[x]))
	for y in range(len(z)):
		test.append(len(m[y]))
	print test

now im trying to think of a way to compare each value in test.im thinking another for loop.
Is there an easier and cleaner way to do this?

okay, i think i got it, but its really long. Is there a better way to do this?

def ma(m):
	z = []
	test = []
	last = []
	ms = len(str(m).split('[')) - 2
	for x in range(ms):
		z.append(len(m[x]))
	for y in range(len(z)):
		test.append(len(m[y]))
	for t in range(len(test)):
		try:
			if test[t] == test[t+1]:
				last.append('yes')
			else:
				last.append('no')
		except IndexError:
			pass
	if 'no' in last:
		print 'nope'
	else:
		print 'yeah'
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.