它说我的代码有一个语法错误,这是我最近添加的新块,它必须在某处出错。我是python的新手,非常感谢任何帮助。也许有缩进的东西,但我不知道在哪里,它没有指定一条线:(
def store_results(class_number, name, score):
class_number = str(class_number) + ".txt" #this adds '.txt' to the end of the file (therefore creating a text file) so it can be used to create a file under the name of the class
file = open(class_number, 'a') #opens the file in 'append' mode so you don't delete all the information
#file.write(str(students_names))
file.write(str(name + " : ")) #writes the name and ":" to file
file.write(str(score)) #writes the score to file
file.write('\n')#writes the score to the file
file.close()#safely closes the file to save the information
def store_results(class_number, name, score):
class_file = "{}.txt".format(class_number) # this adds '.txt' to the end of the file (therefore creating a text file) so it can be used to create a file under the name of the class
with open(class_file, 'a') as f: # opens the file in 'append' mode so you don't delete all the information
f.write("{}: {}\n".format(name, score)
def store_results(class_number, name, score): # this adds '.json' to the end of the file (therefore creating a json file)
class_file = "{}.json".format(class_number) # first step: load the existing data
if not os.path.exists(class_file):
scores = {}
else:
with open(class_file, 'r') as f:
scores = json.load(f)
scores.setdefault(name, []).append(score)
with open(class_file, 'w') as f:
json.dump(scores, f)
错误消息只是说:
**SYNTAX ERROR: INVALID SYNTAX**
但光标会跳回此行
def store_results(class_number, name, score):
答案 0 :(得分:0)
这不能解决您的问题,但您在第20行还有另一个语法错误:
else:
with open(class_file, 'r') as f:
在使用'
之前,有一个不需要的空间编辑: 这是完整的语法错误:
File "check_syntax.py", line 15
def store_results(class_number, name, score):
^
SyntaxError:语法无效
答案 1 :(得分:0)
您的问题与使用格式有关,您有:
f.write("{0}: {1}\n".format(name, score)
你的任务是一个右括号。
f.write("{0}: {1}\n".format(name, score))
这解决了问题,代码通过解释器就好了。我建议使用IDE而不是简单的空闲,提供更多关于问题的洞察力以及正在发生的线路,即使空闲也应该告诉你。
答案 2 :(得分:-1)
我认为问题是因为你有三个同名的函数 - 它是语法错误。