1

I am having trouble to why I am getting this error. I would like to have any assistance with this issue.

happyList = []
sadList = []

exclude = string.punctuation

happy_file = open("happy.txt", "r")
for word in happy_file:
    word = ''.join(ch for ch in word if ch not in exclude)
    happyList.append(word.strip().lower())
happy_file.close()

The error:

    happyList.append(word.strip().lower())
            ^
SyntaxError: invalid syntax
reisdev
  • 3,215
  • 2
  • 17
  • 38
  • Hi. it will be difficult to help you without more context, could you copy/paste the values of `happyList` and `word` just before you get this error? – fmarm Feb 17 '20 at 00:50
  • added it right just now. – undercoverism2001 Feb 17 '20 at 00:52
  • Thanks, you're missing a parenthesis on the line with join after `exclude` – fmarm Feb 17 '20 at 00:53
  • interesting this is invalid syntax error is making me frustrated `while ans: user_input = input("Hello! How are you doing on this fine day?: (or press enter/escape to quit.) ")` – undercoverism2001 Feb 17 '20 at 01:12
  • `user_words = user_input.split() ^ SyntaxError: invalid syntax` user_words the "s" in words is a syntax error – undercoverism2001 Feb 17 '20 at 01:14
  • FYI that's not how you iterate through the contents of a file... – kpie Feb 17 '20 at 01:40
  • I can't reproduce this, I get no syntax error. Please share a [mcve], along with the entire error message/traceback. You might find the following article useful: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. Also, do note that variable and function names should generally follow the `lower_case_with_underscores` style. – AMC Feb 17 '20 at 02:03

2 Answers2

1

For looping the file happy_file, try using the "with" context manager to ensure you are reading properly.

happyList = []
with open('happy-file.txt') as f: 
    input_lines = f.readlines() 
    for i in input_lines: 
       # -- do your processing for each input_line
       happyList.append( ... )
blispr
  • 883
  • 5
  • 10
-1
for word in happy_file:
    word = ''.join(ch for ch in word if ch not in exclude)
    happyList.append(word.strip().lower())

Try putting an else case result as well when you are assigning it to 'word'. I don't remember exactly when, but I had faced a similar issue, when I forgot to give the else case for a similar assignment.

Let me know if it works.

Aditya Bhattacharya
  • 914
  • 2
  • 9
  • 22
  • Just to add why it might happen, (ch for ch in word if ch not in exclude), since this is a shorthand expression, usual way will be to for ... : ; then if ... : ; so because of the ':' missing it might be giving you the syntax error. But I think if you include the else part after the if, it should solve. – Aditya Bhattacharya Feb 17 '20 at 01:44
  • The issue has nothing to do with a missing else statement. In fact, adding an else after the if would **lead to** a syntax error. – AMC Feb 17 '20 at 02:03
  • _Just to add why it might happen, (ch for ch in word if ch not in exclude), since this is a shorthand expression, usual way will be to for ... : ; then if ... : ; so because of the ':' missing it might be giving you the syntax error._ That isn't true either. It's a generator expression, which unlike a list comprehension, cannot really be thought of as a loop in which you append to a list. – AMC Feb 17 '20 at 02:09
  • Refer this one: -- value_when_true if condition else value_when_false It is actually an issue with missing else statement. Once it is added, it will solve the syntax error issue. Please refer this: https://stackoverflow.com/a/2802899 – Aditya Bhattacharya Feb 17 '20 at 16:10
  • That link is about the conditional ternary operator though, which isn't being used here. – AMC Feb 17 '20 at 21:00