Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python project help
#1
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)
   
Reply
#2
Well, that was a bit of a mess  Shocked , 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
Reply
#3
(Jul-20-2017, 03:37 PM)sparkz_alot Wrote: Well, that was a bit of a mess Shocked , 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??
Reply
#4
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
Reply
#5
(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?
Reply
#6
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.
Reply
#7
(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.
Reply
#8
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()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020