如何解决'Invalid character in identifier'?
How to tackle 'Invalid character in identifier'?
“标识符中的字符无效”
我正在 运行ning 一个 python 文件,其中包含 macOS Catalina 上 IDLE 3 中的给定代码。每当我 运行 代码时,它都会显示错误。我无法理解原因。可以的话请guide me
错误显示在 第 11 行 charList
如果我在 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
正如我所见,错误是由第 48 行引起的,您在代码中只写了双引号 (“
)。
这一行:
charList = [] #empty list to fill with random characters
以及所有后续行都使用 non-breaking spaces 而不是常规空格。据推测,您 copy/pasted 来自某些来源(可能是 Web 浏览器或文字处理器)的代码引入了 Python 没有的那个和其他奇特的字符(例如两个评论之间的 “
)认识。
如果您删除该行上的空格并将其重新键入为正常空格,则该行将起作用。但是,您的代码中有超过一百个后续的不间断空格,因此您可能应该从头开始,或者在代码编辑器中进行搜索和替换。
“标识符中的字符无效”
我正在 运行ning 一个 python 文件,其中包含 macOS Catalina 上 IDLE 3 中的给定代码。每当我 运行 代码时,它都会显示错误。我无法理解原因。可以的话请guide me
错误显示在 第 11 行 charList
如果我在 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
正如我所见,错误是由第 48 行引起的,您在代码中只写了双引号 (“
)。
这一行:
charList = [] #empty list to fill with random characters
以及所有后续行都使用 non-breaking spaces 而不是常规空格。据推测,您 copy/pasted 来自某些来源(可能是 Web 浏览器或文字处理器)的代码引入了 Python 没有的那个和其他奇特的字符(例如两个评论之间的 “
)认识。
如果您删除该行上的空格并将其重新键入为正常空格,则该行将起作用。但是,您的代码中有超过一百个后续的不间断空格,因此您可能应该从头开始,或者在代码编辑器中进行搜索和替换。