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

2024-09-28 22:23:30 发布

您现在位置:Python中文网/ 问答频道 /正文

每当我试着输入“线虫”时_小.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

Tags: orname程序input语句file1file2file
1条回答
网友
1楼 · 发布于 2024-09-28 22:23:30

您的代码不正确,因为您使用的是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

相关问题 更多 >