NameError: name ' ' is not defined
NameError: name ' ' is not defined
我有这个程序,我希望它 运行 通过每个菜单选项。基本上用户登录,菜单出现并按下一个工作。但是,当我按 2 时,它告诉我 'Search Student is not defined' 我不明白它在说什么。我试图在程序中移动该函数,但如果这样做,我会遇到其他错误。我正在寻找构建它的正确方法。功能是否在菜单之前?登录后?那我应该怎么打电话给他们呢?
choice = input
def PrintMenu():
print("\n*******************")
print("\n School menu system")
print("\n*******************")
print(" [1] Add Student")
print(" [2] Search Student")
print(" [3] Find Report")
print(" [4] Exit")
choice = input ("Enter a choice: ")
#while choice =='':
if choice == '1':
AddStudent()
elif choice == '2':
SearchStudent(ID)
elif choice == '3':
FindReport()
elif choice == '4':
print("\nExiting the system......\n")
sys.exit(0)
else:
print ("\n not valid choice")
PrintMenu()
def SearchStudent(ID):
with open("Students.txt", 'r') as file:
for i in file:
data = i.rstrip().split(",")
if data[0] == ID:
return "The student you require is: {} {}".format(data[2], data[1])
return "No matches found"
search = input("Please enter a student ID: ")
print(SearchStudent(search))
您应该将 SearchStudent(ID) 放在主函数之前。在 Python 中,事物(例如函数)必须在 定义之前 调用它们。
主要部分必须在您要使用的功能之后。鉴于您的登录功能应该是主要功能,您必须让程序知道它。将此添加到代码的末尾:
if __name__== "__main__":
yourMainFunction()
您的代码很可能如下所示:
def FindReport(args):
#What it's going to do
def SearchStudent(args):
#What it's going to do
def AddStudent(args):
#What it's going to do
def PrintMenu():
#What it's going to do
def Login():
#Your main function
if __name__ == "__main__":
Login()
我有这个程序,我希望它 运行 通过每个菜单选项。基本上用户登录,菜单出现并按下一个工作。但是,当我按 2 时,它告诉我 'Search Student is not defined' 我不明白它在说什么。我试图在程序中移动该函数,但如果这样做,我会遇到其他错误。我正在寻找构建它的正确方法。功能是否在菜单之前?登录后?那我应该怎么打电话给他们呢?
choice = input
def PrintMenu():
print("\n*******************")
print("\n School menu system")
print("\n*******************")
print(" [1] Add Student")
print(" [2] Search Student")
print(" [3] Find Report")
print(" [4] Exit")
choice = input ("Enter a choice: ")
#while choice =='':
if choice == '1':
AddStudent()
elif choice == '2':
SearchStudent(ID)
elif choice == '3':
FindReport()
elif choice == '4':
print("\nExiting the system......\n")
sys.exit(0)
else:
print ("\n not valid choice")
PrintMenu()
def SearchStudent(ID):
with open("Students.txt", 'r') as file:
for i in file:
data = i.rstrip().split(",")
if data[0] == ID:
return "The student you require is: {} {}".format(data[2], data[1])
return "No matches found"
search = input("Please enter a student ID: ")
print(SearchStudent(search))
您应该将 SearchStudent(ID) 放在主函数之前。在 Python 中,事物(例如函数)必须在 定义之前 调用它们。
主要部分必须在您要使用的功能之后。鉴于您的登录功能应该是主要功能,您必须让程序知道它。将此添加到代码的末尾:
if __name__== "__main__":
yourMainFunction()
您的代码很可能如下所示:
def FindReport(args):
#What it's going to do
def SearchStudent(args):
#What it's going to do
def AddStudent(args):
#What it's going to do
def PrintMenu():
#What it's going to do
def Login():
#Your main function
if __name__ == "__main__":
Login()