语法错误消息,在 python 空闲时
Syntax errror message, on python idle
它说我的代码有语法错误,这是我最近添加的新块,它一定有错误。我是 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):
这并没有解决您的问题,但您在第 20 行还有另一个语法错误:
else:
with open(class_file, 'r') as f:
在 'with'
之前有一个不需要的 space
编辑:
这是完整的语法错误:
File "check_syntax.py", line 15
def store_results(class_number, name, score):
^
语法错误:语法无效
您的问题在于您使用格式的方式,您有:
f.write("{0}: {1}\n".format(name, score)
您的任务是右括号。
f.write("{0}: {1}\n".format(name, score))
这解决了问题,并且代码可以很好地通过解释器。我建议使用 IDE 而不是简单的闲置,即使闲置也应该告诉您,但可以更深入地了解问题以及发生的线路。
我认为问题是因为你有三个同名的函数 - 这是语法错误。
它说我的代码有语法错误,这是我最近添加的新块,它一定有错误。我是 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):
这并没有解决您的问题,但您在第 20 行还有另一个语法错误:
else:
with open(class_file, 'r') as f:
在 'with'
之前有一个不需要的 space编辑: 这是完整的语法错误:
File "check_syntax.py", line 15
def store_results(class_number, name, score):
^
语法错误:语法无效
您的问题在于您使用格式的方式,您有:
f.write("{0}: {1}\n".format(name, score)
您的任务是右括号。
f.write("{0}: {1}\n".format(name, score))
这解决了问题,并且代码可以很好地通过解释器。我建议使用 IDE 而不是简单的闲置,即使闲置也应该告诉您,但可以更深入地了解问题以及发生的线路。
我认为问题是因为你有三个同名的函数 - 这是语法错误。