Hello!

I make a program on Python, which counts the number of certain punctuation marks in the text. Faced a problem: the function counts only the first required character on the line, after which it immediately switches to another one - even if there are the same characters in the rest of the line. What is the problem?:(

punct=0 #счетчик for a in line: if '.' in a: punct=punct + 1 print punct 

    5 answers 5

    I don’t have the opportunity to comment, so I’ll add an answer (but it’s still just a comment), I see you are just starting to get familiar with the language, so my advice to you is:

     punct=punct + 1 

    This is the best thing to do.

     punct+=1 

    it won't kill you, but the code will be more readable and respectable.

    with respect.

    though I will throw more in this topic (python 3)

     text.split(".") count = len(text) + 1 

    all the same with respect.

      You can use regexp

       import re text = "hello, mam \n hello, dead, it's very good" pattern = re.compile('.*$', re.M) values = pattern.findall(text) # создает список строк for value in values: # проверяет каждую строку print value.count(',') # считает количество запятых в строке 
      • complicated coming out. count is easier. - mrDoctorWho
      • Well, yes, I thought that the number of commas in each line is necessary. Well, if the proposal, then yes it is necessary text.count (',') - tarasov

      use text.count (symbol)

        Wrong team. It checks for a dot in the string. Those. if it is, addition is triggered. Everything. If you go your way, you should cut off a piece of string along with the found point and repeat the check in the remaining piece ... Although, of course, this is not the best way ...

          based on the text of the problem and the author’s decision code, the most common semantic error is:
          it checks the occurrence of a character in a string, rather than comparing all characters as intended ..
          hence the code should look something like this:

           punct=0 for line in lines: for symbol in line: if symbol==".": punct+=1 print punct 

          or :

           for symbol in bigtext: if ..... 

          You can also use a bunch of other more / less humane ways =)