Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

LoveMathFermat Group Title

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!!!

  • one year ago
  • one year ago

  • This Question is Closed
  1. KonradZuse Group Title
    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.

    • one year ago
  2. TuringTest Group Title
    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).

    • one year ago
  3. snark Group Title
    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...

    • one year ago
  4. TuringTest Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    oh I think that's what I meant :P

    • one year ago
  5. TuringTest Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    oh no, they are different, ic

    • one year ago
  6. LoveMathFermat Group Title
    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:

    • one year ago
  7. LoveMathFermat Group Title
    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?

    • one year ago
  8. Shadowys Group Title
    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. :)

    • one year ago
  9. LoveMathFermat Group Title
    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?

    • one year ago
  10. rsmith6559 Group Title
    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.

    • one year ago
    • Attachments:

See more questions >>>

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.