1
votes

ENVIRONMENT: Windows 7 Python2.7 Eclipse SDK 3.7.2

Trying to follow a tutorial from http://www.youtube.com/watch?v=Z-HpXbhVuGo I receive an error message:

============================= ERRORS ============================= Traceback (most recent call last): File "C:\eclipse\plugins\org.python.pydev_2.7.1.2012100913\pysrc\pydev_runfiles.py", line 432, in get_module_from_str mod = __import(modname) File "C:\Users\lenovo\workspace\assignment3\fileiotest.py", line 17 print(bestStudent[i] + ‘ scored a ‘ + i) ^ SyntaxError: invalid syntax ERROR: Module: fileiotest could not be imported (file: C:/Users/lenovo/workspace/assignment3/fileiotest.py).

.........................................................................................

#-*- coding: utf8 -*-
from __future__ import print_function


bestStudent = {}
f = open ("C:/Users/lenovo/workspace/assignment3/studentgrades.txt")
for line in f:
    name, grade = line.split()
    bestStudent[grade] = name

f.close()

bestStudentStr= "" 

for i in sorted(bestStudent.keys(), reverse=True):
    print bestStudent[i] + 'scored a ' + i
bestStudentStr += bestStudent[i] + ‘ scored a ‘ + i + ‘\n’

bestStudentStr = ‘\nThe Best Students Ranked\n\n’ + bestStudentStr

print(bestStudentStr)

outToFile = open(‘studentrank.txt’, mode=’w', encoding='utf-8′)
outToFile.write(bestStudentStr)

outToFile.close()

print(‘Finished update’)
2
SyntaxError:invalid syntax highlights the following line of code:(bestStudent[i] - stewiestudent
You have some non-ASCII quotes mixed in with ASCII quotes '. Change all of the non-ASCII to ASCII and you should be fine. - mechanical_meat
I also see a backtick ` in there. You will also need to change that to an ASCII quote. - mechanical_meat

2 Answers

0
votes

For the syntax error highlighting bestStudent[i] Replace that print statement with

print (bestStudent[i] + 'scored a ' + i) with ()

Also your code contains a lot of ‘ ’ instead of ' ' single-qoutes which you can simply replace

Fix those issues, I think it ought to work.

0
votes

Non-ascii characters replaced:

from __future__ import print_function

bestStudent = {}
f = open ("C:/Users/lenovo/workspace/assignment3/studentgrades.txt")
for line in f:
    name, grade = line.split()
    bestStudent[grade] = name

f.close()

bestStudentStr= "" 

for i in sorted(bestStudent.keys(), reverse=True):
    print bestStudent[i] + "scored a " + i
bestStudentStr += bestStudent[i] + " scored a " + i + "\n"

bestStudentStr = "\nThe Best Students Ranked\n\n" + bestStudentStr

print(bestStudentStr)

outToFile = open("studentrank.txt", mode="w", encoding="utf-8")
outToFile.write(bestStudentStr)

outToFile.close()

print("Finished update")

This happens when you copy source from pdf,doc etc file. The best solution is to copy/paste in a text editor without unicode support, you'll see then what wrong is as ? marks.