Posts: 19 Threads: 6 Joined: Apr 2017 My code when run is sating 'opt is not defined' #School Register import time def Menu(): print(""" Welcome to SchoolReg 1.Create Register 2.Access Register Press 'Q' to quit""") opt = input(" Please select an option:") def Create(opt): file = open("SchoolRegister.txt","w") file.write(reg_create) def Acess(opt): file = open("SchoolRegister.txt","r") print(file.read()) def Quit(opt): print("Thank you for using SchoolReg, goodbye!") time.sleep(3) quit() Menu() if opt == '1': reg_create = input("Please enter the people present\n") Create(opt) Posts: 1,298 Threads: 38 Joined: Sep 2016 Well, that was a bit of a mess  , lets see if we can partially fix it a bit. # School Register import time # Python loves lower case function names def create(): # Using this method, Python will close the file automatically with open("SchoolRegister.txt", "w") as file: reg_create = input("Please enter the people present\n") file.write(reg_create) def access(): # Using this method, YOU must close the file file = open("SchoolRegister.txt", "r") print(file.read()) file.close() def leave(): print("Thank you for using SchoolReg, goodbye!") time.sleep(3) quit() def menu(): print(""" Welcome to SchoolReg 1.Create Register 2.Access Register Press 'Q' to quit""") opt = input(" Please select an option: ") if opt == '1': create() elif opt == '2': access() elif opt == 'Q': # Python does NOT love using it's keywords, such as 'quit' leave() return menu() If it ain't broke, I just haven't gotten to it yet. OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch" Python 3.6.5, IDE: PyCharm 2018 Community Edition Posts: 19 Threads: 6 Joined: Apr 2017 (Jul-20-2017, 03:37 PM)sparkz_alot Wrote: Well, that was a bit of a mess , lets see if we can partially fix it a bit. # School Register import time # Python loves lower case function names def create(): # Using this method, Python will close the file automatically with open("SchoolRegister.txt", "w") as file: reg_create = input("Please enter the people present\n") file.write(reg_create) def access(): # Using this method, YOU must close the file file = open("SchoolRegister.txt", "r") print(file.read()) file.close() def leave(): print("Thank you for using SchoolReg, goodbye!") time.sleep(3) quit() def menu(): print(""" Welcome to SchoolReg 1.Create Register 2.Access Register Press 'Q' to quit""") opt = input(" Please select an option: ") if opt == '1': create() elif opt == '2': access() elif opt == 'Q': # Python does NOT love using it's keywords, such as 'quit' leave() return menu() What looks wrong?? Posts: 1,298 Threads: 38 Joined: Sep 2016 Not a slam, just an observation. If you compare the code you posted versus the 'cleaned' up code I posted you'll see several changes to your code. I also added a few comments to help clarify. There are still a few things that could be improved and I'm sure they will be addressed as you expand you script. If you have questions about what I did or why, feel free to ask. If it ain't broke, I just haven't gotten to it yet. OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch" Python 3.6.5, IDE: PyCharm 2018 Community Edition Posts: 19 Threads: 6 Joined: Apr 2017 (Jul-20-2017, 04:53 PM)sparkz_alot Wrote: Not a slam, just an observation. If you compare the code you posted versus the 'cleaned' up code I posted you'll see several changes to your code. I also added a few comments to help clarify. There are still a few things that could be improved and I'm sure they will be addressed as you expand you script. If you have questions about what I did or why, feel free to ask. Is there anyway I could include arrays in my code do you think? Posts: 12,117 Threads: 494 Joined: Sep 2016 an array is called a list in python, and is defined as follows: mylist = [] # empty list mylist = ['dogs', 'cats', 'rabbits', 'mice'] or programmatically like: mylist = [] # empty list mylist.append('dogs')to display contents: mylist = ['dogs', 'cats', 'rabbits', 'mice'] for animal in mylist: print(animal) There are many variations of this, and a good read of the docs, or a tutorial would be in order. Posts: 19 Threads: 6 Joined: Apr 2017 (Jul-20-2017, 06:24 PM)Larz60+ Wrote: an array is called a list in python, and is defined as follows: mylist = [] # empty list mylist = ['dogs', 'cats', 'rabbits', 'mice'] or programmatically like: mylist = [] # empty list mylist.append('dogs') to display contents: mylist = ['dogs', 'cats', 'rabbits', 'mice'] for animal in mylist: print(animal) There are many variations of this, and a good read of the docs, or a tutorial would be in order. I know what an array is I just wondered if there was a way that I could implement it in my code. Posts: 12,117 Threads: 494 Joined: Sep 2016 You could use a dictionary: import sys def create(): print('Create routine') def access(): print('access routine') def leave(): sys.exit(0) def menu(): while True: options = {'1': create, '2': access, 'Q': leave} print(""" Welcome to SchoolReg 1.Create Register 2.Access Register Press 'Q' to quit""") opt = input(" Please select an option: ") try: options[opt]() except KeyError: print('Not a valid choice, Try again') menu() |