Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.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_Openfiledialog - Fatal编程技术网

为什么';文件没打开吗?(Python)

为什么';文件没打开吗?(Python),python,openfiledialog,Python,Openfiledialog,每当我尝试输入“C.elegans_small.gff”时,程序就会给出“无法打开”的打印语句。但是,我希望它能打开。为什么会这样 print("Gene length computation for C. elegans.") print() file1 = "C.elegans_small.gff" file2 = "C.elegans.gff" user_input = input("Input a file name: ") while user_input != file1 or us

每当我尝试输入“C.elegans_small.gff”时,程序就会给出“无法打开”的打印语句。但是,我希望它能打开。为什么会这样

print("Gene length computation for C. elegans.")
print()
file1 = "C.elegans_small.gff"
file2 = "C.elegans.gff"
user_input = input("Input a file name: ")
while user_input != file1 or user_input != file2:
    print("Unable to open file.") 
    user_input = input("Input a file name: ")
    if user_input == file1 or user_input == file2:
        break

您的代码不正确,因为您使用的是
而不是

print("Gene length computation for C. elegans.")
print()
file1 = "C.elegans_small.gff"
file2 = "C.elegans.gff"
user_input = input("Input a file name: ")
while user_input != file1 and user_input != file2:
    #now if one is true it exits
    print("Unable to open file.") 
    user_input = input("Input a file name: ")
假设用户输入file1,那么if语句是
False或True

由于它是
而不是
,因此如果其中一条语句是
true
,它仍将处于while循环中。中断while循环的唯一方法是输入同时等于file1和file2

以下是使用
时代码的固定版本

print("Gene length computation for C. elegans.")
print()
file1 = "C.elegans_small.gff"
file2 = "C.elegans.gff"
user_input = input("Input a file name: ")
while user_input != file1 and user_input != file2:
    #now if one is true it exits
    print("Unable to open file.") 
    user_input = input("Input a file name: ")
而且,这部分是无用的。这是因为while循环将检查它并自行中断,因此不需要if语句来中断它

if user_input == file1 or user_input == file2:
        # This stays as or because if one is true you want it to pass
        break

它总是不是一个或不是另一个。将测试反转为
非文件1或非文件2非(文件1和文件2)
,这总是正确的。。。你的意思是
不是file1,也不是file2不是(file1或file2)