我对Python很陌生。我想在下面的代码中找到Python关键字['def','in', 'if'...]
的出现。但是,需要忽略代码中任何字符串常量中的关键字。
在不计算字符串中的关键字的情况下,如何计算关键字的出现次数?在
def grade(result):
'''
if if (<--- example to test if the word "if" will be ignored in the counts)
:param result: none
:return:none
'''
if result >= 80:
grade = "HD"
elif 70 <= result:
grade = "DI"
elif 60 <= result:
grade = "CR"
elif 50 <= result:
grade = "PA"
else:
#else (ignore this word)
grade = "NN"
return grade
result = float(raw_input("Enter a final result: "))
while result < 0 or result > 100:
print "Invalid result. Result must be between 0 and 100."
result = float(raw_input("Re-enter final result: "))
print "The corresponding grade is", grade(result)
使用}模块。在
tokenize
、keyword
和{部分输出:
^{pr2}$您可以从
^{3}$keyword
模块检索关键字列表:集成解决方案集合。计数器公司名称:
相关问题 更多 >
编程相关推荐