为什么';文件没打开吗?(Python)
每当我尝试输入“C.elegans_small.gff”时,程序就会给出“无法打开”的打印语句。但是,我希望它能打开。为什么会这样为什么';文件没打开吗?(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
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)