0

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

shell returned 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()

1 Answer 1

1

Edit:

You are already returning result from depunctuateTokens, so just do this inside 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:

  1. The syntax requires you to.

  2. It will noticeably improve the clarity of the code.

Otherwise, they are just redundant characters.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok that fixed that. Now I get an error saying the global name result is not defined. Is there a way that I can put the result I get from the depunctuate function into the decapitalize function?
Ok thanks for being so helpful to a total beginner. Is there a way I could map the decapitalize function over all the letters in a token?
@user3321218 - I don't quite know what you mean...but I am sure that it is possible. You should ask a new question to specifically deal with that topic (after all, this question was about the SyntaxError). Doing so will allow you to get the best help. Don't forget though to be clear when explaining your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.