0
user_word = input("guess a word: ")

with open("C:/Users/Callum_test/Documents/Python/dog.txt") as dictionary:
    if user_word is dictionary.read():
        print("Well done, you got a", len(user_input), "letter word")

    else:
        print("That's not a word!")

Hello,

I've created the above code but for some reason it can't find the word in the file? I've not done anything like this for a while and have no idea how to get it working. The code functions seemingly fine but no input is found in the text file.

thanks, Callum

3
  • 1
    you may need to strip any newline characters off the users input first try user_word = input("guess a word: ").strip('\n') Commented Jan 29, 2019 at 18:47
  • cheers for the reply but that's still not worked. Do you have any other ideas? Commented Jan 29, 2019 at 18:49
  • 1
    Ah my mistake, you have a typo is, change to in: if user_word in dictionary.read(): Commented Jan 29, 2019 at 18:51

1 Answer 1

1

Your code had many mistakes, how you were doing it is fine, just you messed up here and there. First off you didn't properly open the file, you need to set the mode as read(r):

with open("C:/Users/Callum_test/Documents/Python/dog.txt", "r") as dictionary:

Next you said "is" instead of "in" if you want to check if something is IN a file, list, string etc...

    if user_word in dictionary.read():

Finally, you changed user_word to user_input for some reason

        print("Well done, you got a", len(user_word), "letter word")

Here is the fixed and completed code:

user_word = input("guess a word: ")

with open("C:/Users/Callum_test/Documents/Python/dog.txt", "r") as dictionary:
    if user_word in dictionary.read():
        print("Well done, you got a", len(user_word), "letter word")
    else:
        print("That's not a word!")

Hope this helped

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks thats great
No problem, I'm happy to help, if you wouldn't mind marking it as correct would be great.

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.