I am a student who is new to python and need to finish a python program for assignment. The goal of the program is to find some target strings in a list of news titles.
For example, there is a file written with a list of news title. When I type this in command line:
"python hw3.py Obama#Trump,Japan#anime,Hearthstone#Overwatch new_list.txt" ,
the program will return the numbers of titles which have both "Obama" and "Trump" in them, and also which have both "Japan" and "anime" in them, and so on.
However, when I was debugging my program, it seems that the command line can't read the symbol "#" because the words after # are all red, and the result when entering the line went "Error:syntax invalid".
Was the problem caused by the "#" symbol? Because typing "#" is the requirement of the assignment, so I must find a way to make it right. Or the problem was caused by other reason?
The code is here:
import sys
if len(sys.argv) < 3:
print 'no argument'
sys.exit()
f = open(sys.argv[2], 'r', encoding = 'UTF-8')
words = sys.argv[1].split(',')
count = len(words)
for i in range(0, count, 1):
yes[count] = 0
word = words[count].decode('utf-8').split('#')
while True :
x = f.readline()
if x == '':
break
#print(x,end='')
else if (word[0] in x) and (word[1] in x):
yes[count] += 1
for j in range (0, count, 1):
t = max(yes)
for jk in range (0, count, 1):
if yes[jk] == t:
print words[jk] + ',' + yes[jk]
yes[jk] = -1
break
f.close()
And the message is here:
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> python TocHW3.py 馬英九#蔡英文 new_sample.txt
SyntaxError: invalid syntax