Python 如何应对';标识符';中的字符无效;?

Python 如何应对';标识符';中的字符无效;?,python,invalid-characters,Python,Invalid Characters,“标识符中的字符无效” 我正在macOS Catalina上运行一个包含IDLE 3中给定代码的python文件。每当我运行代码时,它都会显示一个错误。我无法理解原因。如果可以,请指导我 错误显示在第11行字符列表中 如果我在makeList()函数中删除第9,10行的注释,那么错误将出现在第10行 我听说过双重报价的问题,但这不是问题所在 注:我正在阅读Peter Farrell的《Python数学冒险》一书,第12章 import random target = "I never

“标识符中的字符无效”

我正在macOS Catalina上运行一个包含IDLE 3中给定代码的python文件。每当我运行代码时,它都会显示一个错误。我无法理解原因。如果可以,请
指导我

错误显示在第11行字符列表中
如果我在makeList()函数中删除第9,10行的注释,那么错误将出现在第10行

我听说过双重报价的问题,但这不是问题所在

注:我正在阅读Peter Farrell的《Python数学冒险》一书,第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
随机导入
target=“我从不食言,因为那是我的忍者之道。”
characters=“abcdefghijklmnopqrstuvwxyzabefghijklmnopqrstuvxyz.”,?!”
#函数创建与目标长度相同的字符“猜测”列表。
def makeList():
''返回长度相同的字符列表
作为目标“”
charList=[]#用随机字符填充的空列表
对于范围内的i(len(target)):
charList.append(随机选择(字符))
返回字符表
#函数通过将猜测列表与目标进行比较来“打分”
def分数(mylist):
''返回一个整数:与目标''的匹配数'
匹配项=0
对于范围内的i(len(target)):
如果mylist[i]==目标[i]:
匹配项+=1
复赛
#函数通过随机更改一个字母来“改变”列表
def mutate(mylist):
''返回我的列表,其中一个字母已更改''
新建列表=列表(mylist)
新字母=随机。选择(字符)
索引=random.randint(0,len(目标)-1)
新列表[索引]=新字母
返回新列表
#创建一个列表,将该列表设置为最佳列表
#将bestList的分数设置为bestScore
random.seed()
bestList=makeList()
最佳得分=得分(最佳列表)
猜测=0
#创建一个无限循环,该循环将创建一个变异
#在最佳名单中,给它打分
尽管如此:
猜测=变异(最佳列表)
猜分数=分数(猜)
猜测+=1
#如果新列表的得分低于最佳列表,
“
#创建一个列表,将该列表设置为最佳列表
#将bestList的分数设置为bestScore
random.seed()
bestList=makeList()
最佳得分=得分(最佳列表)
猜测=0
#创建一个无限循环,该循环将创建一个变异
#在最佳名单中,给它打分
尽管如此:
猜测=变异(最佳列表)
猜分数=分数(猜)
猜测+=1
#如果新列表的得分低于最佳列表,
#“继续”到循环的下一个迭代

如我所见,如果guessScore,则错误是由第48行引起的,您在代码中只写了双引号(
)。

这一行:

    charList = [] #empty list to fill with random characters
所有后续行都使用了空格,而不是常规空格。可能是您从某个源代码(可能是web浏览器或文字处理器)复制/粘贴了代码,该源代码引入了Python无法识别的字符和其他奇特字符(如两条注释之间的


如果删除该行上的空格并将其作为普通空格重新键入,则该行将起作用。但是,随后代码中有100多个不间断的空格,因此您可能应该从头开始,或者在代码编辑器中进行搜索和替换。

请将代码作为文本而不是图像发布。@Jacques还添加了代码请发布有关错误的所有信息。此奇特的引号字符
似乎出现在您的代码中两条注释之间。没有明显的原因,它肯定会导致某种错误。我知道它是什么。您的代码中有空格。删除该行上的空格并键入普通空格。然后,当您在随后的每一行上都出现类似错误时,也在那里执行。无论在哪里复制/粘贴代码时,引入了Python无法识别的奇怪字符。我删除了双引号,但错误仍然存在