我的验证,以检查我加载到程序中的文件是一个正方形或不工作,我不知道为什么,并希望一些帮助+指导和解释,以便我可以学习。你知道吗
问题是将一个文件加载到python中,然后检查该文件是否是nxn正方形。你知道吗
我做错了什么?我该如何修复它?你知道吗
代码:
while True:
try:
filesname = input("Enter the filename:") + ".txt"
file = open(filesname,"r")
readFile = file.readlines()
file.close()
print ("File has:")
thelist = []
square(thelist)
for line in readFile:
thelist.append(line)
print (line, end="")
square(thelist)
except:
print ("The file name you have entered does not exist. Please try again.")
except:
print ("")
print ("Incorrect file format")
else:
break
我让它工作,但当我运行程序时,我得到:
File contents:
---------------
ABC
BAC
CAB
LJ
Incorrect file format
Enter the filename:
这是可行的,但是我如何改变程序,使它甚至不打印出文件,如果它不是在正确的格式?我试过玩弄它,但弄不明白。你知道吗
打印前需要调用
square()
:相关问题 更多 >
编程相关推荐