0

I have this program and I would like it to run through each menu option. Basically the user logs in, the menu appears and pressing one works. However when I press 2 it tells me that 'Search Student is not defined' I don't understand what it is talking about. I have tried to move the function around in the program but I get other errors if I do that. I am looking for what is the right way to structure this. Do the functions go before the menu? and after the login? then how can and should I call them up?

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))
Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
CV9
  • 5
  • 1
  • 1
  • 7

1 Answers1

0

You should place SearchStudent(ID) before your main function. In Python, things (such as functions) must be defined before you call them.

The main section must go after the functions you're going to use. Given your login function should be the main function, you must let the program know it. Add this to the end of your code:

if __name__== "__main__":
  yourMainFunction()

Your code will most likely look like this:

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()
  • I don't understand where the main section in the code is though or where it needs to go. Before the menu there is a login function and then there is the addstudent function and I've tried putting it before and after the menu and it still comes up with the same error. – CV9 Sep 23 '17 at 21:02
  • @cᴏʟᴅsᴘᴇᴇᴅ any ideas? or suggestions? – CV9 Sep 23 '17 at 21:03
  • I updated the answer with further help. – Rodrigo Pereira Sep 24 '17 at 00:16
  • I've tried it using your structure and it's still bringing up student search first. It should be the login and then the menu. I called them all up in the main section too, was I suppossed to do that? – CV9 Sep 24 '17 at 07:03
  • Hmmm... if you mean the "if __name__" part by main, then that's why. Login should be the only one there, or else it's going to call all functions at once (and, in your case, given student search was likely the first function that main received, it called it first). – Rodrigo Pereira Sep 24 '17 at 11:22
  • I put Login and Menu in the if_name_=="main": so under this I did Login() PrintMenu() – CV9 Sep 24 '17 at 11:42
  • I'm getting this error when I search for a student by choosing 2 on the menu SearchStudent() TypeError: SearchStudent() missing 1 required positional argument: 'ID' – CV9 Sep 24 '17 at 11:42
  • Hmmm... well, first things first. Menu should not be in if __name__ == "__main__". Only Login() should be there, so make sure to remove it. As for your error, are you sure the variable ID exists? I looked at your code, and it's nowhere to be seen (so far, I've only spotted choice), so that can be why it's throwing that error. I'd recommend creating an ID input variable if the user chooses 2. – Rodrigo Pereira Sep 24 '17 at 12:01
  • Thank you for your help so far. Would the variable be declared in the menu or in the student search function? Also I removed the print menu from the main and it didn't do anything after it logged me in, so it calls the login function but nothing else, its so confusing. – CV9 Sep 24 '17 at 12:32
  • You're welcome. As for your question, it must be declared in the menu, otherwise it'll result in the same error (given your function won't receive anything). As for your login function, it may be because there's nothing in it that links to your PrintMenu function. My suggestion is to call PrintMenu() inside it if the login is successful. – Rodrigo Pereira Sep 24 '17 at 13:11
  • So I've just ran it and Login in Main works and at the bottom of Login I called the print menu. I have now got it to work by doing exactly what you have said and moving the search = input to the menu system. Perfect I have a few more things to do to finish the program. I need to loop that menu for one. Thank you!! I will mark it as answered and I appreciate all the help and support given. – CV9 Sep 24 '17 at 14:43
  • You're very welcome! Glad to help. – Rodrigo Pereira Sep 24 '17 at 14:58