Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果语句顺序错误,不确定是什么错误_Python_Python 3.x_If Statement - Fatal编程技术网

Python 如果语句顺序错误,不确定是什么错误

Python 如果语句顺序错误,不确定是什么错误,python,python-3.x,if-statement,Python,Python 3.x,If Statement,在try中使用def nChar时,它不工作: 当我输入文件时,例如: while True: try: enterName = input("Enter file name:") + ".txt" openFile = open(enterName,"r") read = openFile.readlines() openFile.close() puzzle = [] for line

在try中使用def nChar时,它不工作:

当我输入文件时,例如:

while True:
    try:

        enterName = input("Enter file name:") + ".txt"
        openFile = open(enterName,"r")
        read = openFile.readlines()
        openFile.close()
        puzzle = [] 
        for lines in read: 
            puzzle.append(lines.strip())

        square(puzzle) 



    print ("\nFile loading...")
    print ("\nFile:")
    for letters in puzzle:
        print (letters)

    checked(puzzle)
它直接说“文件没有n个不同的字符。” 我做错了什么?if语句的位置是否错误?或者您的
nChar()
函数始终返回
None

ABC
BCA
CAB
None
在布尔上下文中被视为false

让函数返回
True
False

def nChar(nc):
    grid = len(nc)
    for char in nc:
        if len(set(char)) != grid: 
            return

啊,好的,谢谢。我这样做了,我运行了程序,我遇到了另一个问题。当我输入文件名时。e、 g file1它不断地再次询问文件名,而不是打印出来并执行其他保存功能等,这最好作为一个新问题发布;这个具体问题已经解决了。确保你能拿出一份合适的报告;在这个问题中,您发布了不完整的代码(除block外,没有
),因此无法查看您的文件处理出现了什么问题。您是否尝试过调试?@mangeshhotage我修复了它,但目前我正试图处理另一个错误。New post for New error。张贴/接受此问题的正确答案。
def nChar(nc):
    grid = len(nc)
    for line in nc:
        if len(set(line)) != grid: 
            return False
    return True