[英]python else statement syntax error
when I run this, I get the following error: Does anybody know what might be causing this? 运行此命令时,出现以下错误:有人知道是什么原因吗? The purpose of this program is to create an array, remove all punctuation from the array, and remove all lowercase characters from the array
该程序的目的是创建一个数组,从该数组中删除所有标点符号,并从该数组中删除所有小写字符
File "words.py", line 37 else: ^ SyntaxError: invalid syntax 文件“ words.py”,第37行,否则:^ SyntaxError:语法无效
shell returned 1 外壳退回1
import sys
from scanner import *
arr=[]
def main():
print("the name of the program is",sys.argv[0])
for i in range(1,len(sys.argv),1):
print(" argument",i,"is", sys.argv[i])
tokens = readTokens("text.txt")
cleanTokens = depunctuateTokens(arr)
words = decapitalizeTokens(result)
def readTokens(s):
s=Scanner("text.txt")
token=s.readtoken()
while (token != ""):
arr.append(token)
token=s.readtoken()
s.close()
return arr
def depunctuateTokens(arr):
result=[]
for i in range(0,len(arr),1):
string=arr[i]
cleaned=""
punctuation="""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
for i in range(0,len(string),1):
if string[i] not in punctuation:
cleaned += string[i]
result.append(cleaned)
print(result)
return result
def decapitalizeTokens(result):
if (ord(result) <= ord('Z')):
return chr(ord(result) + ord('a') - (ord('A'))
else:
return result
main()
Edit: 编辑:
You are already returning result
from depunctuateTokens
, so just do this inside main
: 您已经从
depunctuateTokens
返回了result
,因此只需在main
内部执行此操作:
cleanTokens = depunctuateTokens(arr)
words = decapitalizeTokens(cleanTokens)
You need a closing parenthesis: 您需要一个右括号:
return chr(ord(result) + ord('a') - (ord('A'))
# here--^
Or, you can remove the extra opening parenthesis: 或者,您可以删除多余的左括号:
return chr(ord(result) + ord('a') - (ord('A'))
# here--^
Personally, I would recommend the later solution. 就个人而言,我建议使用稍后的解决方案。 You should only use parenthesis if:
仅在以下情况下才应使用括号:
The syntax requires you to. 语法要求您。
It will noticeably improve the clarity of the code. 它将显着提高代码的清晰度。
Otherwise, they are just redundant characters. 否则,它们只是多余的字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.