我写了一个新函数,当我执行它时,我收到一个错误:
def add(x, y):
z = x / y * 100
return z
def calc_grade(perc):
if perc < 50:
return "1"
if perc < 60:
return "2"
if perc < 75:
return "3"
if perc < 90:
return "4"
if perc >= 90:
return "5"
def calc_command():
num1 = input("Input your points: ")
num2 = input("Input maximum points: ")
num3 = add(float(num1), float(num2))
grade = calc_grade(num3)
print("This is your result:", str(num3) + "%")
print("Your grade:", grade)
save = open("grades.txt", "r")
read_grades = save.read()
save = open("grades.txt", "w")
save.write(read_grades + grade)
save.close()
def read_grades():
save = open("grades.txt", "r")
read_grades = save.read()
grades = read_grades.split()
save.close()
return grades
while True:
command = input("Input your command: ")
if command == "CALC":
calc_command()
elif command == "EXIT":
break
elif command == "GRADES":
print(read_grades)
elif command == "HELP":
print("These are the commands:\nCALC - Calculates your grade and writes in the file.\nEXIT - Exits the program.\nGRADES - Reads your previous grades.\nHELP - This command. It helps you.")
else:
print("You inputed an invalid command. Type HELP for help.")
好的,这是我的代码:
{{1}}
执行read_grades()函数或GRADES命令时会发生此错误。
对于那些标记这个问题的人:我做了一些搜索,我没有找到那篇帖子,现在我读了它,我不明白答案
答案 0 :(得分:6)
这不是运行时错误,您打印了一个函数
print(read_grades)
尝试改为调用
read_grades()
你在这里覆盖你的功能
read_grades = save.read()
因此,建议不要使用与您的函数名冲突的变量名