问题
I wrote a new function and when I execute it, I get an error:
<function read_grades at 0x000001F69E0FC8C8>
Ok so here is my code:
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.")
This error happens when I execute the read_grades() function or the GRADES command.
For those who marked this question: I did some searching and I didn't find that post and now that i read it i dont understand the answer
回答1:
That's not a runtime error, you printed a function
print(read_grades)
Try calling it instead
read_grades()
And you override your function here
read_grades = save.read()
So, advice is to not use variable names that conflict with your function names
来源:https://stackoverflow.com/questions/38041011/python-function-at-0x-output