Python <0x 处的函数>输出 [英] Python &lt;function at 0x&gt; output

查看:56
本文介绍了Python <0x 处的函数>输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个新函数,当我执行它时,出现错误:

I wrote a new function and when I execute it, I get an error:

好的,这是我的代码:

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.")

当我执行 read_grades() 函数或 GRADES 命令时会发生此错误.

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

推荐答案

那不是运行时错误,你打印的是一个函数

That's not a runtime error, you printed a function

print(read_grades)

尝试调用它

read_grades() 

你在这里覆盖你的功能

read_grades = save.read()

所以,建议不要使用与函数名冲突的变量名

So, advice is to not use variable names that conflict with your function names

这篇关于Python <0x 处的函数>输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆