Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I was working on the HP code wars 2012 distribution problem. I was continuously getting an error message that says an invalid character in the identifier. What does this mean and how I can improve that? Here is the code:

import  string

def  text_analyzer(text):

'''The text to be parsed and

the number of occurrences of the letters given back

be. Punctuation marks and I ignore the EOF

simple. The function is thus very limited.

'''

    result =  {}

# Processing

    for  a in  string.ascii_lowercase:

    result [a] =  text.lower (). count (a)

    return  result

def  analysis_result (results):

# I look at the data

    keys =  analysis.keys ()

    values   =  list(analysis.values   ())

    values.sort (reverse = True )

# I turn to the dictionary and

# Must avoid that letters will be overwritten

    w2 =  {}

    list =  []

    for  key in  keys:

        item =  w2.get (results [key], 0 )

        if  item = =  0 :

            w2 [analysis results [key]] =  [key]

        else :

            item.append (key)

            w2 [analysis results [key]] =  item

# We get the keys

    keys =  list (w2.keys ())

    keys.sort (reverse = True )

    for  key in  keys:

        list =  w2 [key]

        liste.sort ()

        for  a in  list:

            print (a.upper (), "*"  *  key)        

text =  """I have a dream that one day this nation will rise up and live out the true

meaning of its creed: "We hold these truths to be self-evident, that all men

are created equal. "I have a dream that my four little children will one day

live in a nation where they will not be judged by the color of their skin but

by the content of their character.

# # # """

analysis result =  text_analyzer (text)

analysis_results (results)

 

1 Answer

0 votes
by (108k points)

The SyntaxError that you are getting as an invalid character in the identifier means you have some string in the middle of a variable name, methods, etc. The exact error message will be something like this:

  File "invalchar.py", line 23

    values =  list(analysis.values ())

                ^

SyntaxError: invalid character in identifier

The error only will tell you where the actual problem is. If you see that message, there are a group of non-printing garbage characters. Just remove them, you will remove the error.

If your python editor doesn't give you a method to find and fix those strings, just delete and retype the line.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Mar 1, 2021 in Python by Rekha (2.2k points)
0 votes
1 answer
asked Jan 5, 2021 in Python by ashely (50.2k points)
0 votes
1 answer
asked Jan 18, 2020 in Python by Rajesh Malhotra (19.9k points)

Browse Categories

...