Reputation: 1
Having problems with my code and cannot seem to fix it or know where i have gone wrong. any help will be appreciated.
it runs but doesn't go any further than the user inputting the text file name.
it is supposed to read the user input and from that read a text file and declare whether it is a magic puzzle or not, i wanted this to read 5x5 as well but i'm a bit lost on how to do it
column = 0
row = 0
data = []
def main():
file = input("Enter filename :")
while True:
try:
f = open(file+".txt","r")
break
except:
file = input("Enter filename :")
for line in f.readline():
numbers = line.split(' ')
cube = [int(x) for x in numbers]
is_magic(x)
def is_magic(x):
if not dupe(x) and check_sum(x):
print ('Valid')
else:
print ('Invalid')
def dupe(x):
if len(x) == len(set(x)):
return False
return True
def check_sum(x):
if vertical_check(x) and horizontal_check(x) and diagonal_check(x):
return True
return False
def vertical_check(x):
if sum(x[0:9:3]) == sum(x[1:9:3]) == sum(x[2:9:3]) == 15:
return True
return False
def horizontal_check(x):
if sum(cube[0:3]) == sum(cube[3:6]) == sum(cube[6:9]) == 15:
return True
return False
def diagonal_check(x):
if sum(cube[0:9:4]) == sum(cube[2:7:2]) == 15:
return True
return False
def writeFile(x):
f = open("VALID_"+x+".txt","w")
text = ""
for a in data:
for x in a:
text = text+str(x)+" "
text = text+"\n"
f.write(text)
f.close()
return
main()
'''
txt file 3x3
2 9 4
7 5 3
6 1 8
Upvotes: 0
Views: 218
Reputation: 64
the reason for the program being stuck is the break
statement after opening the file. This break statement will exit the while loop, so the rest of the code will not be executed.
f = open(file+".txt","r")
break # <<<< remove this
Upvotes: 1
Reputation: 461
The break statement inside while True makes you break the loop.
while True:
try:
f = open(file+".txt","r")
break
Upvotes: 1