Create a sign-out sheet in Python

  • Thread starter Adam Kohnle
  • Start date
  • Tags
    Python
In summary: Incorrect input. Please enter the name of a student in the list.') elif student not in list_of_students[index]: print('Incorrect input. Please enter the name of a student in the list.') elif student not in student_list: print('Incorrect input. Please enter the name of a student in the list.') else: print('{}: {}'.format(i, student))
  • #1
Adam Kohnle
21
5
Hello everyone, I am trying to create a sign out sheet as if I am a teacher and its so then people can sign out electronically. I know very little python and I have googled most things. I have never worked with a text file before. I am trying to make so it will ask my name, destination, and time and it will print it like this:

Adam Kohnle Bathroom 1:21and then after I close the python program, others can use the same python program and also sign out so it can look like this:

Adam Kohnle Bathroom 1:21
John Cena Nurse 1:33

but my problem is that everytime I relaunch the python program, it instead replaces the text previously typed into that text file. How do I make so it keeps the data the first student typed in and just adds the second students data? Here is my code so far:
Python:
# file-output.py
f = open('Sign Out Sheet.txt','w')
f.write(input('What is your full name?'))
f.write('')
destination = input('What is your destination?')
f.write(destination)
f.write('')
f.write(input('What is the time?'))
f.write('')
f.close()
Also I have no idea what the # file-output.py is. I know it is a comment but I just copied it off of online and I have been just heavily editing the initial code. Well heavily is relative, what I mean is it was originally this:
Python:
# file-input.py
f = open('helloworld.txt','r')
message = f.read()
print(message)
f.close()
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Also another thing I want to do later is have it create a pass for you that you can just print and then leave the class room. I want it to automatically put in the information
 
  • #3
i figured out how to make it just add the text without deleting everything. I just needed an "a" instead of a "w" in the line that states:

f = open('Sign Out Sheet.txt','w')

Now I just want it to create a pass for me as well that once it is generated, I can simply print using simply ctrl + p.
 
  • #4
Adam Kohnle said:
i figured out how to make it just add the text without deleting everything. I just needed an "a" instead of a "w" in the line that states:

f = open('Sign Out Sheet.txt','w')

Now I just want it to create a pass for me as well that once it is generated, I can simply print using simply ctrl + p.
Is this a toy project for learning python, or is this a project you actually intend to be useful by others?
 
  • #5
it is just for practice but if it ends up good enough, my technology teacher wants to use it
 
  • #6
To develop good software you have to think about the big picture:
- Who is the target audience?
- how will they interact with the computer?
- what skills and background do you expect them to have?
- What kind of data do you expect from them and what is the likelihood and consequences of erroneous data?
- etc

The problem of software design to solve a specific problem is completely separate from how to accomplish a given software solution in a specific programming language such a python. I don't want to discourage you but from what you have described so far, it is unlikely to be useful for more than learning python. I would recommend you spend your time going through a decent python tutorial first to learn the language, and then only after that attempt to solve specific problems in python. I could post some code but it won't be useful to you if you do not understand the language constructs.

What was the specific task given to you by your technology teacher? Is this part of homework?
 
  • #7
The teacher did not assign it, I simply finished all of his assignments early so I created my own project. I then told him what I was doing and he mentioned that if it works well enough then he will want to use it so students can sign out of the classroom using their computers. I want the audience to have no experience with python so they can just run it using the f5 button and then type in the destination, their name, the time out and then print the hall pass
 
  • #8
To read a name from the command line you should do something like:

Python:
name = input('Please enter your name: ')

For Python2 you should use raw_input because input expects you to enter "Adam Kohnle" instead of Adam Kohnle without the quotes which is not user-friendly.

Python:
name = raw_input('Please enter your name: ')

One thing to consider is that it is very easy to make mistakes with typing names so instead it is better to read a class list of all students and print their names with a number next to it and ask for the number instead of the names. The after you get the number, print the selected name and ask for confirmation before proceeding to the next task. For example:

Python:
for i, student in enumerate(list_of_students):
    print('{}: {}'.format(i, student))
index = int(raw_input("Please enter the number next to your name: "))
selected_student = list_of_students[index]
confirmed = raw_input("{}: Do you want to sign-out? Y/N :".format(selected_student))

I see that you are also asking for the time to be entered. This is not wise, since the computer already knows the time, and students can make mistakes entering the time, so simply record the current time and don't ask for it. You can use the datetime module to do that. For example

Python:
from datetime import datetime
current_time = datetime.now() # current time including date

To append to the file open it with 'a' instead of 'w' as you already noted.
 
  • #9
lodbrok said:
To read a name from the command line you should do something like:

Python:
name = input('Please enter your name: ')

For Python2 you should use raw_input because input expects you to enter "Adam Kohnle" instead of Adam Kohnle without the quotes which is not user-friendly.

Python:
name = raw_input('Please enter your name: ')

One thing to consider is that it is very easy to make mistakes with typing names so instead it is better to read a class list of all students and print their names with a number next to it and ask for the number instead of the names. The after you get the number, print the selected name and ask for confirmation before proceeding to the next task. For example:

Python:
for i, student in enumerate(list_of_students):
    print('{}: {}'.format(i, student))
index = int(raw_input("Please enter the number next to your name: "))
selected_student = list_of_students[index]
confirmed = raw_input("{}: Do you want to sign-out? Y/N :".format(selected_student))

I see that you are also asking for the time to be entered. This is not wise, since the computer already knows the time, and students can make mistakes entering the time, so simply record the current time and don't ask for it. You can use the datetime module to do that. For example

Python:
from datetime import datetime
current_time = datetime.now() # current time including date

To append to the file open it with 'a' instead of 'w' as you already noted.
That is extremely helpful! Thanks! How would you approach making it generate the pass as well? I thought about having it open in google chrome but I do not know how that works. I tried google but my idea is pretty specific so I was having trouble finding an answer. I was hoping it would use an image of a pass and simply add the rest of the information but I do not know how to do it.
 
  • #10
Adam Kohnle said:
How would you approach making it generate the pass as well? I thought about having it open in google chrome but I do not know how that works.
That is not a straightforward thing and is getting a bit complicated for your level. Nevertheless, there are specific python libraries out there for dealing with printing labels, which you could use as passes. See for example https://pypi.python.org/pypi/pylabels/1.0.0 or https://pypi.python.org/pypi/brother_ql.

But your level of python would need to improve to be able to understand how they work, and how to use them.
 
  • #11
lodbrok said:
That is not a straightforward thing and is getting a bit complicated for your level. Nevertheless, there are specific python libraries out there for dealing with printing labels, which you could use as passes. See for example https://pypi.python.org/pypi/pylabels/1.0.0 or https://pypi.python.org/pypi/brother_ql.

But your level of python would need to improve to be able to understand how they work, and how to use them.
Thanks and I am the type of person that learns through a trial by fire metaphorically. When I do something for my level then I typically get bored but if it is beyond my level, I see where I want to be and then I become more interested and typically I learn faster. It sounds counter intuitive but everyone learns in their own way I guess
 
  • #12
Also I want to emphasize how much I appreciate the help! You have been extremely helpful and I appreciate it!
 
  • Like
Likes berkeman
  • #13
Adam Kohnle said:
Thanks and I am the type of person that learns through a trial by fire metaphorically. When I do something for my level then I typically get bored but if it is beyond my level, I see where I want to be and then I become more interested and typically I learn faster. It sounds counter intuitive but everyone learns in their own way I guess

OK, I figure the best way for you to learn is to give you something simple but close enough with unfinished components for you to fill in. Your tasks are:
1. Generate the appropriate files, run it make sure it works
2. Describe how the program works
3. Implement the sections marked as TODO, see the comments for hints

If you are able to do all of the above three things, you will end up with something that maybe usable and will learn a lot about python in the process. Still, I recommend going through a python tutorial.

Python:
import csv
from datetime import datetime
import osclass SignOutApp(object):
    def __init__(self, students_file, signouts_file):
        """
        Initialize the application
            students_file - name of csv file with columns 'first_name', 'last_name' of students
            signouts_file - name of csv file in which signout data is stored.
        """
        self.signouts_file = signouts_file
        self.students_file = students_file
        self.students = self.load_students()
        self.signouts = self.load_signouts()
 
    def load_students(self):
        """
        Read a csv file with columns 'first_name', 'last_name' of students
        """
        with open(self.students_file, 'r') as csvfile:
            reader = csv.DictReader(csvfile)
            students = ['{first_name} {last_name}'.format(**row) for row in reader]
        return students
 
    def load_signouts(self):
        """
        Read a csv file with columns 'student', 'time', 'purpose' of signouts
        """
        if os.path.exists(self.signouts_file):
            # signouts exist read existing file
            with open(self.signouts_file, 'r') as csvfile:
                reader = csv.DictReader(csvfile)
                signouts = [row for row in reader]
        else:
            # no signouts exists, return an empty list
            signouts = []
        return signouts
 
    def save_signouts(self):
        """
        Save a csv file with columns 'student', 'time', 'purpose' of signouts
        """
    
        with open(self.signouts_file, 'w') as csvfile:
            writer = csv.DictWriter(csvfile, fieldnames=['student', 'time', 'purpose'])
            writer.writeheader()
            for row in self.signouts:
                writer.writerow(row)
 
    def print_pass(self, signout):
        """
        Print a for the given signout
            - signout: a dictionary with fields 'student', 'time', 'purpose'
        """
        # TODO : Generate and print the pass. You can use the zpl2 library
        #        https://simple-zpl2.readthedocs.io/en/latest/usage.html
    
 
    def run(self):
        """
        Register  Signouts, save file after each entry
        """    
    
        while True:
            print('----- New Sign-Out ----- [Ctrl-C to quit]')
            # TODO: print the names of the students, with a number next to each name
            #       you can use the PTable library https://pypi.python.org/pypi/PTable/0.9.0
        
            index = int(input('Please enter the number next to your name: '))        
            student = self.students[index]
            confirm = input('Does {} want to sign-out? [Y]/N:  '.format(student)) or "Y"
            if confirm.lower() == 'n':
                continue
            else:
                purpose = input("{}, enter the purpose for sign-out: ".format(student))
                timestamp = datetime.now().isoformat()
                signout = {
                    'student': student,
                    'time': timestamp,
                    'purpose': purpose
                }
            
                # TODO: print a formatted copy of the signout on screen. You can also
                #       use PTable for this or anything else you like
            
                self.print_pass(signout)
                self.signouts.append(signout)
                self.save_signouts()if __name__ == '__main__':
    app = SignOutApp('grade10.csv', 'grade10_signouts.csv')
    try:
        app.run()
    except KeyboardInterrupt:
        print('Exiting ...')
 
Last edited:
  • #14
lodbrok said:
OK, I figure the best way for you to learn is to give you something simple but close enough with unfinished components for you to fill in. Your tasks are:
1. Generate the appropriate files, run it make sure it works
2. Describe how the problem works
3. Implement the section marked as TODO, see the comments for hints

If you are able to do all of the above three things, you will end up with something that maybe usable and will learn a lot about python in the process. Still, I recommend going through a python tutorial.

Python:
import csv
from datetime import datetime
import osclass SignOutApp(object):
    def __init__(self, students_file, signouts_file):
        """
        Initialize the application
            students_file - name of csv file with columns 'first_name', 'last_name' of students
            signouts_file - name of csv file in which signout data is stored.
        """
        self.signouts_file = signouts_file
        self.students_file = students_file
        self.students = self.load_students()
        self.signouts = self.load_signouts()
 
    def load_students(self):
        """
        Read a csv file with columns 'first_name', 'last_name' of students
        """
        with open(self.students_file, 'r') as csvfile:
            reader = csv.DictReader(csvfile)
            students = ['{first_name} {last_name}'.format(**row) for row in reader]
        return students
 
    def load_signouts(self):
        """
        Read a csv file with columns 'student', 'time', 'purpose' of signouts
        """
        if os.path.exists(self.signouts_file):
            # signouts exist read existing file
            with open(self.signouts_file, 'r') as csvfile:
                reader = csv.DictReader(csvfile)
                signouts = [row for row in reader]
        else:
            # no signouts exists, return an empty list
            signouts = []
        return signouts
 
    def save_signouts(self):
        """
        Save a csv file with columns 'student', 'time', 'purpose' of signouts
        """
     
        with open(self.signouts_file, 'w') as csvfile:
            writer = csv.DictWriter(csvfile, fieldnames=['student', 'time', 'purpose'])
            writer.writeheader()
            for row in self.signouts:
                writer.writerow(row)
 
    def print_pass(self, signout):
        """
        Print a for the given signout
            - signout: a dictionary with fields 'student', 'time', 'purpose'
        """
        # TODO : Generate and print the pass. You can use the zpl2 library
        #        https://simple-zpl2.readthedocs.io/en/latest/usage.html
     
 
    def run(self):
        """
        Register  Signouts, save file after each entry
        """     
     
        while True:
            print('----- New Sign-Out ----- [Ctrl-C to quit]')
            # TODO: print the names of the students, with a number next to each name
            #       you can use the PTable library https://pypi.python.org/pypi/PTable/0.9.0
         
            index = int(input('Please enter the number next to your name: '))         
            student = self.students[index]
            confirm = input('Does {} want to sign-out? [Y]/N:  '.format(student)) or "Y"
            if confirm.lower() == 'n':
                continue
            else:
                purpose = input("{}, enter the purpose for sign-out: ".format(student))
                timestamp = datetime.now().isoformat()
                signout = {
                    'student': student,
                    'time': timestamp,
                    'purpose': purpose
                }
             
                # TODO: print a formatted copy of the signout on screen. You can also
                #       use PTable for this or anything else you like
             
                self.print_pass(signout)
                self.signouts.append(signout)
                self.save_signouts()if __name__ == '__main__':
    app = SignOutApp('grade10.csv', 'grade10_signouts.csv')
    try:
        app.run()
    except KeyboardInterrupt:
        print('Exiting ...')

Thanks! I will see what I can do!
 
  • #15
go to the codeacadamy. They have a very good python tutorial that will help you a lot
 
  • #16
donpacino said:
go to the codeacadamy. They have a very good python tutorial that will help you a lot

I have done a decent bit of python on code academy but because I took a class, I am ahead of where I am on code academy and I don't like playing catch up but your right I should.
 

Related to Create a sign-out sheet in Python

1. How do I create a sign-out sheet in Python?

To create a sign-out sheet in Python, you will need to first import the necessary libraries, such as pandas and openpyxl. Then, you can use the pandas DataFrame function to create a table, and use the openpyxl library to save the table as an Excel file.

2. Can I customize the sign-out sheet with specific columns and formatting?

Yes, you can customize the sign-out sheet by specifying the columns you want to include and using formatting functions such as set_column_width() and cell_range(). This will allow you to create a professional-looking sign-out sheet with your desired layout.

3. How do I add new entries to the sign-out sheet?

You can add new entries to the sign-out sheet by using the append() function in pandas. This will add a new row to the bottom of the existing table, allowing you to input new data as needed.

4. Is it possible to automatically sort the sign-out sheet by a specific column?

Yes, you can use the sort_values() function in pandas to sort the sign-out sheet by a specific column in either ascending or descending order. This can be useful for organizing the data and making it easier to read and analyze.

5. How can I export the sign-out sheet as a PDF file?

To export the sign-out sheet as a PDF file, you can use the xlsx2pdf library in Python. This library allows you to convert Excel files into PDF format, which can be helpful for sharing the sign-out sheet with others who may not have access to Excel.

Similar threads

Replies
6
Views
699
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
21
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
4
Views
437
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top