为什么文件没有打开? (蟒蛇)

时间:2018-02-25 01:18:18

标签: python openfiledialog

每当我尝试输入" C.elegans_small.gff"时,程序会给出打印声明"无法打开"。但是,我想要它打开。为什么会这样?

(a,b)

1 个答案:

答案 0 :(得分:1)

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

假设用户输入file1,那么if语句为False or True

由于它是or而不是and,如果其中一个语句是true,它仍然会在while循环中。从while循环中断的唯一方法是输入是否同时等于file1和file2。

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

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
相关问题