简体   繁体   English

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

[英]Why doesn't the file open? (Python)

Whenever I try to input "C.elegans_small.gff", the program gives the print statement of "unable to open". 每当我尝试输入“ C.elegans_small.gff”时,程序都会给出“无法打开”的打印语句。 However, I want it to open. 但是,我要打开它。 Why is this happening? 为什么会这样呢?

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

Your code is incorrect because you are using or instead of and . 您的代码不正确,因为您使用or代替and

Let's say the user inputs file1 then the if-statement is False or True . 假设用户输入file1,则if语句为False or True

Since it is or and not and , if one of the statements is true it will still be in the while loop. 既然是orand ,如果语句之一是true它仍然是在while循环。 The only way to break from the while loop is if the input is equal to file1 and file2 at the same time. 打破while循环的唯一方法是同时输入等于file1和file2。

Here is a fixed version of your code when using and . 这是使用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: ")

Also, this part is useless. 另外,这部分是没有用的。 This is because the while loop will check it and break by itself so you do not need an if statement to break it. 这是因为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM