0

Whenever I try to input "C.elegans_small.gff", the program gives the print statement of "unable to open". 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
2
  • 1
    It is always not the one OR not the other. Invert the test to and Commented Feb 25, 2018 at 1:20
  • not file1 or not file 2 <=> not (file1 and file2) which is always true... You mean not file1 and not file2 <=> not(file1 or file2) Commented Feb 25, 2018 at 1:38

1 Answer 1

1

Your code is incorrect because you are using or instead of and.

Let's say the user inputs file1 then the if-statement is 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. The only way to break from the while loop is if the input is equal to file1 and file2 at the same time.

Here is a fixed version of your code when using 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.

if user_input == file1 or user_input == file2:
        # This stays as or because if one is true you want it to pass
        break
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.