Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

LoveMathFermat

  • 2 years ago

def opening_files(words): file = open("words.txt") judge=False for line in file: if words == line: judge = True break return judge Hi! Fellow OCW students! Using a code above, I'm trying to return True if there's a match inside of my text file, but even if I put a word that is in the file, it keeps returning False. Can anybody spot a error on my code? Thanks!!!

  • This Question is Closed
  1. KonradZuse
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I'm assuming your "words == line" returns false, meaning words does NOT or "!=" line.

  2. TuringTest
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I'm not positive, but usually when you open a text file like that it is in the form of a string of characters, so "for line in file" is perhaps treating each "line" as a character (hence no character is being checked as a word). If you have a text file and you want to get the words into some kind of list you could use the .split() method (look it up in the python standard library if you don't know it).

  3. snark
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    use line.strip() - it is reading line + '\n', if words == line.strip() : # this works for me...

  4. TuringTest
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    oh I think that's what I meant :P

  5. TuringTest
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    oh no, they are different, ic

  6. LoveMathFermat
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank you so much for your help guys! line.strip() # this works, but I don't know why this works though. This is the definition of stip(), but I don't understand how strip works in my code. Strip([char]): Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:

  7. LoveMathFermat
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I have one more question. I want my code to recognize both lowercase and uppercase. How can I do that?

  8. Shadowys
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    you can use the lower() function to turn them all to lower case first. :)

  9. LoveMathFermat
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    AttributeError: 'file' object has no attribute 'lower' # I have an errors message when using lower() Also, I want it to recognize both lowercase and uppercase, not just lowercase. Is that possible?

  10. rsmith6559
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    lower() is a method of the string object, not the file object, so "FOO".tolower() is "foo". If you're doing string comparisons, they are done case sensitively. As far as the computer is concerned, strings are just a series of one or two byte (depending on the character encoding) integers.

  11. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.