Hello,
I am a 1st semester Computer Science student in a Python class. Our current assignment calls for us to read a list from a file, create a 2-dimensional list from the file, and check to see whether or not 2-dim list constitutes a "magic square" (i.e. all the rows, columns and diagonals equal each other when added). We are to create several definitions, and I have had no problem reading from the file, creating the list, and getting the sum of the first row. The next thing I need to do is sum up the other rows and see if they are equal to the first row. If not equal, I return a false and the function ends, if all are equal, I return a True. When I try to call another function (def sum(list, dimensions)) in my "areRowSumsEqua lToSum" function, I am getting a message telling me that the term "dimensions " is not defined, even though i use the same term in the "def sum(list, dimensions)" function. When I try to call sum(list, dimensions) in the definition name, I get an "invalid syntax" error.
The list file looks like this, the first line is the dimensions of the "square", with the list following after a blank space.
3
8
1
6
3
5
7
4
9
2
Here is my code, I appreciate any help, and I apologize if this is a mess. I'm pretty green still.
[CODE=python]
def main():
import string
value = True
while value:
try:
userfile = raw_input("Ente r the file name where the list is located: ")
infile = open(userfile, 'r')
value = False
except IOError:
print "File not found"
dimensions = eval(infile.rea dline())
infile.readline ()
list = construct2dimli st(infile, dimensions)
print sum(list, dimensions)
print areRowSumsEqual ToSum(list, sum(list, dimensions))
def construct2dimli st(infile, dimensions):
outerlist = []
for i in range(dimension s):
sublist = []
for j in range(dimension s):
sublist = sublist + [eval(infile.rea dline())]
outerlist = outerlist + [sublist]
return outerlist
print construct2dimli st(infile, dimensions)
infile.close
def sum(list, dimensions):
rowsum = 0
for i in range(dimension s):
rowsum = rowsum + list[0][i]
return rowsum
def areRowSumsEqual ToSum(list, sum(list, dimensions)):
rowsums = 0
for i in range(1, len(list)):
for j in range(len(list) ):
rowsums = rowsums + list[i][j]
if(sum(list) != rowsums):
return False
return True
main() [/CODE]
Thanks!
edit: i guess the indents don't show up here?
I am a 1st semester Computer Science student in a Python class. Our current assignment calls for us to read a list from a file, create a 2-dimensional list from the file, and check to see whether or not 2-dim list constitutes a "magic square" (i.e. all the rows, columns and diagonals equal each other when added). We are to create several definitions, and I have had no problem reading from the file, creating the list, and getting the sum of the first row. The next thing I need to do is sum up the other rows and see if they are equal to the first row. If not equal, I return a false and the function ends, if all are equal, I return a True. When I try to call another function (def sum(list, dimensions)) in my "areRowSumsEqua lToSum" function, I am getting a message telling me that the term "dimensions " is not defined, even though i use the same term in the "def sum(list, dimensions)" function. When I try to call sum(list, dimensions) in the definition name, I get an "invalid syntax" error.
The list file looks like this, the first line is the dimensions of the "square", with the list following after a blank space.
3
8
1
6
3
5
7
4
9
2
Here is my code, I appreciate any help, and I apologize if this is a mess. I'm pretty green still.
[CODE=python]
def main():
import string
value = True
while value:
try:
userfile = raw_input("Ente r the file name where the list is located: ")
infile = open(userfile, 'r')
value = False
except IOError:
print "File not found"
dimensions = eval(infile.rea dline())
infile.readline ()
list = construct2dimli st(infile, dimensions)
print sum(list, dimensions)
print areRowSumsEqual ToSum(list, sum(list, dimensions))
def construct2dimli st(infile, dimensions):
outerlist = []
for i in range(dimension s):
sublist = []
for j in range(dimension s):
sublist = sublist + [eval(infile.rea dline())]
outerlist = outerlist + [sublist]
return outerlist
print construct2dimli st(infile, dimensions)
infile.close
def sum(list, dimensions):
rowsum = 0
for i in range(dimension s):
rowsum = rowsum + list[0][i]
return rowsum
def areRowSumsEqual ToSum(list, sum(list, dimensions)):
rowsums = 0
for i in range(1, len(list)):
for j in range(len(list) ):
rowsums = rowsums + list[i][j]
if(sum(list) != rowsums):
return False
return True
main() [/CODE]
Thanks!
edit: i guess the indents don't show up here?
Comment