Demo entry 5034213

Attendance Program

   

Submitted by Simon Boettcher on Jun 01, 2016 at 03:15
Language: Python. Code size: 8.6 kB.

# This is a small Python 2.7 attendance roll program.
# Author: Simon Boettcher
# Thanks: Morris Bernstein, Zed Shaw
# Date created: 15/05/2013
# Last updated: 22/05/2013
# Times updated: 7

# Assignations
a = True # This makes 'a' true.
x = True # This makes 'x' true.
y = True # This makes 'y' true.

# Program intro.
print "\n"
print "Roll Initiated"

# Teacher Startup Wizard, asking for prerequisite information from teacher or staff.
print "\n"
print "Teacher, Please enter the current date in DD.MM.YYYY format."
while a == True:
    date = raw_input ("Date: ")
    #Checks whether user input is higher or lower than 10 characters, if so prints 'invalid statement'.
    if len(date) < 10 or len(date) > 10:
        print "Invalid date, try again."
        print "\n"
        continue

    # Checks whether the date format is correct.
    try:
        with open(date + '.dat', 'w'): pass
        a = False
    except IOError:
        print "Date invalid format, please try again."
        print "\n"
        a = True
# Writes attendance roll intro into file.
target = open(date + '.dat', 'w') # This assigns 'date'.dat as the writable target.
target.write("Class Attendance Roll")
target.write("\n")
target.write("\n")
target.write("Date: ")
target.write(date)
target.write("\n")
target.write("\n")
target.write("Students: ")
target.write("\n")
print "Startup Wizard complete."
print "\n"
print "NOTE: Please type the 'help' command for functions."

# Student Wizard, asking for student code or command.
while x == True:
    print "\n"
    print "Please enter your student code or a command."
    response = raw_input ("Student Code/Command: ")
    # Checking if response is a quit command and then exiting if it is.
    if response == "exit":
        x = False
    else:
        # Checks whether user input is higher or lower than 6 characters.
        if len(response) < 7 or len(response) > 7:
            # Checks if reponse is a command.
            if response == "open" or response == "read" or response == "help" or response == "clear" or response == "absent" or response == "delete" or response == "remove": # or response == "restart"
                if response == "delete" or response == "remove":
                    print "Which file date's file file would you like to delete (DD.MM.YYYY)?"
                    remove = raw_input("Date: ")
                    print "Are you sure you want to delete %d from database?" % remove
                    remove_sure = raw_input("y/n: ")
                    if remove_sure == "y" or remove_sure == "yes":
                        import os
                        os.remove(remove + '.dat')
                        print "File %s was deleted from database." % remove
                    else:
                        continue
                # Checks if response is a 'restart' command.
                if response == "restart":
                    # Asks user if they're certain they want to restart the program.
                    print "Are you should you would like to restart the program?"
                    restart = raw_input("y/n: ")
                    if restart == "y" or restart == "yes":
                        # Opens second copy of program.
                        import os
                        os.startfile(open_date + '.dat')
                        # Exits current copy of program.
                        print "Restarting..."
                        x = False
                    else:
                        print "Restart cancelled."
                        continue
                # Checking if response is an 'absent' command.
                if response == "absent":
                    print "\n"
                    print "Which student is absent?"
                    absent = raw_input("Student Code: ")
                    print "Are you sure that %s is absent (y/n)?" % absent
                    absent_check = raw_input("y/n:")
                    print "Why is %s absent?" % absent
                    absent_reason = raw_input("Reason: ")
                    if absent_check == "y" or absent_check == "yes":
                        target.write("\n")
                        target.write("NOTE: ")
                        target.write("\n")
                        target.write(absent + " = absent")
                        target.write("\n")
                        target.write("Reason: " + absent_reason)
                        target.write("\n")
                        target.write("\n")
                        print "%s has been marked absent." % absent
                    else:
                        continue
                # Checking if response is a clear command.
                if response == "clear":
                    print "\n"
                    print "What date's roll would you like to clear (DD.MM.YYYY)?"
                    clear_date = raw_input ("Date: ")
                    # Checks whether file exists.
                    try:
                        with open(clear_date + '.dat'): pass
                    except IOError:
                        print "File does not exist."
                        continue
                    # Prompts user whether they're sure they wish to clear file.
                    print "Are you sure you want to clear %s's file (y/n)?" % clear_date
                    clear_sure = raw_input("y/n:")
                    # If user is sure clear specified date's file.
                    if clear_sure == "y":
                        clear = open(clear_date + '.dat', 'w')
                        clear.truncate()
                        print "%s's roll cleared." % clear_date
                    else:
                        continue
                # Checking if response is a read command.
                if response == "read":
                    # Asks which file user would wish to view.
                    print "\n"
                    print "What date's roll would you like to view (DD.MM.YYYY)?"
                    print "NOTE: To view the current date you must restart the program."
                    read_date = raw_input ("Date: ")
                    # Checks whether file exists.
                    try:
                        with open(read_date + '.dat'): pass
                    except IOError:
                        print "File does not exist."
                        continue
                    # Displays contents of selected '.dat' file
                    from sys import argv
                    f = open(read_date + '.dat')
                    print "\n"
                    print f.read()
                # Checking if response is an open command.
                if response == "open":
                    print "\n"
                    print "What date's roll would you like to manually edit (DD.MM.YYYY)?"
                    open_date = raw_input ("Date: ")
                    # Checks whether file exists.
                    try:
                        with open(open_date + '.dat'): pass
                    except IOError:
                        print "File does not exist."
                        continue
                    # Opens specified date's file.
                    import os
                    os.startfile(open_date + '.dat')
                    print "Now editing %s." % open_date
                # Checks whether response is help, if so displays help.
                if response == "help":
                    print "\n"
                    print "Commands:"
                    print "Mark student as absent = absent"
                    print "View/read previous date's file = read"
                    print "Open previous date's file for editing = open"
                    print "Deletes/removes the specified date's file = delete"
                    print "Clear the contents of a date's file = clear"
                    print "Exit/close the program = exit"
                    # print "Restart/reboot the program = restart"
                    continue
            # Prints 'invalid statment' after user input if applicable.
            else:
                print "Invalid student code or command."
                continue
        # Writes student code to target '.dat' file.
        else:
            student_code = response
            target.write(student_code + " = present")
            target.write("\n")

# Tells user data is saved before program exits.
print "\n"
print "Data saved."

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).