我不明白为什么我的python代码显示警告?

时间:2020-07-11 07:15:22

标签: python

我正在学习Python编码。我今天面临一个问题。我的代码正确显示输出,但显示警告。我不知道哪里出了问题。请帮我解决。

代码:

class Info:
    Name = ""
    Roll = ""
    Section = ""
    Department = ""
    Session = ""
    University = ""


def display(a, b, c, d, e, f):
    print(f"Name: {a}")
    print(f"ID: {b}")
    print(f"Section: {c}")
    print(f"Department: {d}")
    print(f"Session: {e}")
    print(f"University: {f}")


Code = input("Enter Code: ")
Code = Info()  # Error in this line
Code.Name = input("Enter Name: ")
Code.Roll = input("Enter ID: ")
Code.Section = input("Enter Section Name: ")
Code.Department = input("Enter Department Name: ")
Code.Session = input("Enter Session: ")
Code.University = input("Enter University Name: ")
display(Code.Name, Code.Roll, Code.Section, Code.Department, Code.Session, Code.University)

此行Code = Info()

中显示错误

错误消息:
enter image description here

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

IDE的linter发出的警告消息告诉您:

Redeclared "Code" defined above without usage.

Code由您的input()函数调用定义。但是随后您可以通过调用Code立即再次定义Info(),而无需使用调用input()的结果。

答案 1 :(得分:1)

因为您要在连续2行中重新分配相同的变量(代码)。

您可以删除第一行

代码=输入。...

答案 2 :(得分:1)

显示警告消息是因为您定义了一个名为Code的变量,您将输入存储在此行中:

Code = input("Enter Code: ")

但是您实际上从未使用过它,因为您在下一行中对其进行了重新定义:

Code = Info()

您注意到这可能不会引起任何错误,但是许多现代代码编辑器会警告您有关未使用的变量的信息。在您的情况下,您应该问自己,用户输入的目的是什么,为什么不在任何地方使用它?