#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0

    Programming help on strings


    I am writing this program to count letters when the user asks to count a letter in a file. The letter the user asks to count should not be case-sensitive. For example, if the user wishes to count the letter "a", the program should count "A" and "a" as the same thing and vise versa. I figured out one part of the program, but I am now stuck. Can you guys give me some hints? I am new to programming, too. Thanks!

    Code:
    def m():
    enter = input("Enter a letter: ")
    fname = input("Enter filename: ")
    infile = open(fname, "r" )

    for i in (infile):
    if (ord(enter) >=97 or ord(enter)<=122) :
    infile.lower()
    print (infile.count(enter))

    elif (ord(enter) >= 65 or ord(enter) <= 90) :
    infile.upper()
    print(infile.count(enter))
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,895
    Rep Power
    481

    your logic impedes a correct program


    Just convert the character and the text of the file to a common case, then count the characters. Something like this:
    Code:
    def m(text, character):
        return text.lower().count(character.lower())
    
    def main():
        enter = input("Enter a letter: ")
        fname = input("Enter filename: ")
        with open(fname) as inf:
            return m(inf.read(),enter)
    
    if __name__ == '__main__':
        print(main())
    this program delivered with insufficient testing.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Just convert the character and the text of the file to a common case, then count the characters. Something like this:
    Code:
    def m(text, character):
        return text.lower().count(character.lower())
    
    def main():
        enter = input("Enter a letter: ")
        fname = input("Enter filename: ")
        with open(fname) as inf:
            return m(inf.read(),enter)
    
    if __name__ == '__main__':
        print(main())
    this program delivered with insufficient testing.
    Thanks for your help! But I haven't learned that much functions, or is there another to do this using something beginners would use? Thanks!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,895
    Rep Power
    481
    You have str.lower and you have str.count .
    What else did you think you need?
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    Originally Posted by b49P23TIvg
    You have str.lower and you have str.count .
    What else did you think you need?
    I rewrote my code with the advice I got from you and this is what I got. I know there are still errors.
    Code:
    def m(text,character):
        text.lower()
        return text.count(character.lower())
    
    def main():
        enter = input("Enter a letter: ")
        fname = input("Enter filename: ")
        infile = open(fname, "r")
    
        for line in (infile):
            print(m(line,enter))
    
        infile.close()
    main()
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,895
    Rep Power
    481
    The sort method of list works "in place". That is, it arranges the objects in the list.

    Strings are immutable. They cannot be changed.

    Code:
    def m(text,character):
        text.lower()
    The "text.lower()" statement produces a new string with all lower case letters. You haven't assigned that new string to a variable; the change is lost. Hmm. Suppose m were like this?
    Code:
    def m(text,character):
        '''
            >>> m('The quick brown fox...', '.')  # three dots in first argument
            3
            >>> m('Ttabc','T')  # two "tees" in first argument
            2
        '''
        lower_case_text = text.lower()
        lower_case_char = character.lower()
        return lower_case_text.count(lower_case_char)
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    Originally Posted by b49P23TIvg
    The sort method of list works "in place". That is, it arranges the objects in the list.

    Strings are immutable. They cannot be changed.

    Code:
    def m(text,character):
        text.lower()
    The "text.lower()" statement produces a new string with all lower case letters. You haven't assigned that new string to a variable; the change is lost. Hmm. Suppose m were like this?
    Code:
    def m(text,character):
        '''
            >>> m('The quick brown fox...', '.')  # three dots in first argument
            3
            >>> m('Ttabc','T')  # two "tees" in first argument
            2
        '''
        lower_case_text = text.lower()
        lower_case_char = character.lower()
        return lower_case_text.count(lower_case_char)
    Oh, thank you! I forgot that strings cannot be changed, and must be copied into a new string for that to work. Also, I have another question. I want to sum up all of the occurrences to one number. I know I need to use a for loop, but every case I try doesn't seem to work. What is the mistake in my program? Thanks, for your help!

    Code:
    def m(text,character):
        lowtext = text.lower()
        lowchar = character.lower()
        return lowtext.count(lowchar)
    
    def main():
        enter = input("Enter a letter: ")
        fname = input("Enter filename: ")
        infile = open(fname, "r")
        
        for line in (infile):
             g=m(line,enter)
        
        for i in g:
            sum(i)
    main()
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,895
    Rep Power
    481
    Code:
    def main():
        enter = input("Enter a letter: ")
        fname = input("Enter filename: ")
        infile = open(fname, "r")
    
        total = 0
    
        for line in (infile):
             g = m(line, enter)
             total += g            # same as    total = total + g
    
        # now do something with total
        return total

    Comments on this post

    • ckwan48 agrees
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    def main():
        enter = input("Enter a letter: ")
        fname = input("Enter filename: ")
        infile = open(fname, "r")
    
        total = 0
    
        for line in (infile):
             g = m(line, enter)
             total += g            # same as    total = total + g
    
        # now do something with total
        return total
    I tried using print instead of return and it work, however when I tried using return it wouldn't return a value. Is there a problem I am not seeing? Thanks!
    Code:
    def m(text,character):
        lowtext = text.lower()
        lowchar = character.lower()
        return lowtext.count(lowchar)
    
    def main():
        enter = input("Enter a letter: ")
        fname = input("Enter filename: ")
        infile = open(fname, "r")
        total=0
        
        for line in (infile):
             h=m(line,enter)
             total = total +h
        print(total)
    
        
        
    main()
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,895
    Rep Power
    481
    Probably everything's fine.

    If you enter 4 into the interpreter it responds 4 .

    If you have a module containing the single statement 4 and you load the module it won't display 4 .
    [code]Code tags[/code] are essential for python code and Makefiles!
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Probably everything's fine.

    If you enter 4 into the interpreter it responds 4 .

    If you have a module containing the single statement 4 and you load the module it won't display 4 .
    I'm getting confused about what you're saying. I still don't get why it won't return a value if I use return. Thanks!
  22. #12
  23. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,895
    Rep Power
    481
    You must provide exact details about what you've done for diagnosis.

    What operating system did you use?
    What commands did you use to start python?
    How did you run your program?

    Or you could just put the return value statement back into your main program, then call it like

    print(main())

    and you'll see that main did return a value,
    and that you can now see the result.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo