我正在处理HP Code wars 2012中的字母分配问题。我不断收到一条错误消息,指出标识符中的无效字符.这是什么意思,以及如何解决.这是带有信息的页面. hpcodewars.org/past/cw15/problems/2012ProblemsFinalForPrinting.pdf 这是代码
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 \u200b\u200b= list(analysis.values \u200b\u200b())
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)
最新回答
- 2021-1-111 #
- 2021-1-112 #
如果您的键盘是 设置为美国英语(国际)而不是美国英语,双引号无效.这就是在您的情况下单引号起作用的原因.
- 2021-1-113 #
不确定这是正确的,但是当我复制一些有关使用pgmpy的代码并将其粘贴到Spyder下的编辑器中时,尽管没有,但我仍然收到"标识符中的无效字符"错误 看起来对我不好.特定的行是
grade_cpd = TabularCPD(variable='G',\
我没有充分的理由更换了
'
与"
整个代码中,并且有效.不知道为什么,但是它确实起作用 - 2021-1-114 #
如果您只运行模块,则在IDLE中不会收到良好的错误消息.尝试从IDLE shell中键入导入命令,您将获得更多有用的错误消息.我犯了同样的错误,而这一切都与众不同。
(是的,我是从电子书中复制代码的,其中充满了看不见的"错误"字符。)
- 2021-1-115 #
仔细查看您的报价,这是对还是错! 有时双引号无法正常工作,这取决于您的键盘布局。
相关问题
- python:素因数分解-列表pythonpython3.xprimefactoring2021-01-12 01:25
- windows下适用于Python 3x的OpenCVpythonwindowsopencvpython3.x2021-01-11 22:58
- python:TypeError:worker()接受0个位置参数,但给出了1个pythonpython3.x2021-01-11 22:58
- 如何在Python 3x中获得类似2x的排序行为?pythonpython3.xsortingpython2.x2021-01-11 07:27
- python:定义__eq__的类型是不可散列的吗?pythonhashpython3.x2021-01-11 05:54
错误
SyntaxError: invalid character in identifier
表示您在变量名称,函数等中间有一些字符,而不是字母,数字或下划线.实际的错误消息将如下所示:这可以告诉您实际的问题是什么,因此您不必猜测"我的无效字符在哪里"? 好吧,如果您看那行,那儿会有一堆非print垃圾字符.把它们拿出来,你就会过去的。
如果您想知道实际的垃圾字符是什么,我从您的代码中复制了有问题的行,并将其粘贴到Python解释器中的字符串中:
所以这就是
\u200b
或零宽度空间.这就解释了为什么您无法在页面上看到它.最常见的原因是,您已经从CoDebug或wiki之类的网站上复制了某些格式化(非纯文本)代码,或者从PDF文件中复制了这些代码。如果您的编辑器没有提供查找和修复这些字符的方法,只需删除并重新输入该行即可。
当然,您也至少有两个
IndentationError
是因为不缩进东西,至少又增加了SyntaxError
来自停留空间(例如= =
代替==
)或下划线变成空格(例如analysis results
代替analysis_results
)。问题是,您如何使代码进入这种状态? 如果您使用Microsoft word之类的代码作为代码编辑器,那就是您的问题.使用文本编辑器.如果不是,那么,根本的问题是导致您最终得到这些垃圾字符,缩进的缩进和多余的空格,请在尝试修复代码之前对其进行修复。