pythoninvalid-characters

How to tackle 'Invalid character in identifier'?


"Invalid character in identifier"

I am running a python file containing the given code in IDLE 3 on macOS Catalina. Whenever i run the code, it shows an error. I am unable to understand the reason. Please guide me if you can.

The error is showing at line 11 charList
If I remove the comment from line 9,10 in makeList() function, then error comes at line 10 for

I have heard about the problem of double quotation but that's not the problem here.

Note: I am following the book "Math Adventures With Python" by Peter Farrell, chapter 12

import random

target = "I never go back on my word, because that is my Ninja way."
characters = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.',?!"


#function to create a "guess" list of characters the same length as target.
def makeList():
    '''Returns a list of characters the same length
    as the target'''
    charList = [] #empty list to fill with random characters
    for i in range(len(target)):
        charList.append(random.choice(characters))
    return charList

#function to "score" the guess list by comparing it to target
def score(mylist):
    '''Returns one integer: the number of matches with target'''
    matches = 0
    for i in range(len(target)):
        if mylist[i] == target[i]:
            matches += 1
    return matches

#function to "mutate" a list by randomly changing one letter
def mutate(mylist):
    '''Returns mylist with one letter changed'''
    newlist = list(mylist)
    new_letter = random.choice(characters)
    index = random.randint(0,len(target)-1)
    newlist[index] = new_letter
    return newlist

#create a list, set the list to be the bestList
#set the score of bestList to be the bestScore
random.seed()
bestList = makeList()
bestScore = score(bestList)

guesses = 0

#make an infinite loop that will create a mutation
#of the bestList, score it
while True:
    guess = mutate(bestList)
    guessScore = score(guess)
    guesses += 1

#if the score of the newList is lower than the bestList,
“
#create a list, set the list to be the bestList
#set the score of bestList to be the bestScore
random.seed()
bestList = makeList()
bestScore = score(bestList)

guesses = 0

#make an infinite loop that will create a mutation
#of the bestList, score it
while True:
    guess = mutate(bestList)
    guessScore = score(guess)
    guesses += 1

#if the score of the newList is lower than the bestList,
#"continue" on to the next iteration of the loop
    if guessScore <= bestScore:
        continue

#if the score of the newlist is the optimal score,
#print the list and break out of the loop
    print(''.join(guess),guessScore,guesses)
    if guessScore == len(target):
        break

#otherwise, set the bestList to the value of the newList
#and the bestScore to be the value of the score of the newList
    bestList = list(guess)
    bestScore = guessScore

Solution

  • This line:

        charList = [] #empty list to fill with random characters
    

    and all subsequent lines are using non-breaking spaces instead of regular spaces. Presumably you copy/pasted your code from some source (a web browser or a word processor perhaps) that introduced that and other fancy characters (such as the between two comments) that Python does not recognise.

    If you delete the spaces on that line and retype them as normal spaces, that line will work. However, you then have over a hundred subsequent non-breaking spaces in your code, so you should probably either start from scratch, or do a search and replace in your code editor.