Sign up for a school club.

School club

Try

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

x = y.readline()

x is assigned to be a single line read from file pointer y. This will include the end of line character code.

for x in y

Can be used to iterate through file pointer y a line at time. The variable x will contain a single line of data read from the file in each iteration. Note there is no need to use the readline method with this approach.

Data in text files is often stored in comma separated value (CSV) format. E.g.

"red",255,0,0
"green",0,255,0
"blue",0,0,255

A single line is one record with each field of the record separated with a comma. Using the data above and the following code:

record = file.readline()
record = record.strip()
fields = record.split(",")

Would result in:

fields[0] is "red"
fields[1] is 255
fields[2] is 0
fields[3] is 0

Investigate

Questions to think about with this program to check your understanding:

Purpose question

What is the purpose of the iteration in line 18?

for student in file:
Reveal answer

To read in the students line by line from the beginning to the end of the file.

Reason question

Why is "a" used instead of "w" in line 9 to open the file?

file = open(programming + ".txt", "a")
Reveal answer

New students need to be added to the file. Using “a” means that new data is appended (added to the end) and does not over-write existing data in the file.

Make

Change the program so that it:

  1. Asks the user for the name of the club before adding or outputting the students. The club can only be programming, football or drama.
  2. Writes and reads a different file for each club. The name of the club is the filename. E.g. football.txt would be the football club.

Typical inputs and outputs from the program would be:

1. Sign up
2. Show students
Enter choice: 1
Clubs available: programming, football or drama
Enter the name of the club: programming
Enter your name to sign up for the club: Craig
You have been signed up Craig
1. Sign up
2. Show students
Enter choice: 1
Clubs available: programming, football or drama
Enter the name of the club: football
Enter your name to sign up for the club: Dave
You have been signed up Dave
1. Sign up
2. Show students
Enter choice: 2
Clubs available: programming, football or drama
Enter the name of the club: football
Students that signed up for the football club:
Dave
Sam
Mo

Restricted automated feedback

Automated feedback for this assignment is still under construction. Submitted programs are checked for syntax errors and their source code is checked for potential errors, bugs, stylistic issues, and suspicious constructs. However, no checks are performed yet to see if the program correctly implements the behaviour specified in the assignment.

🆘 If you're really stuck, use this Parsons code sorting exercise
add_student
# Add a student to the club
def add_student(club):
---
    student = input("Enter your name to sign up for the club: ")
---
    student = student + "\n"
---
    file = open(club + ".txt", "a")
---
    file.write(student)
---
    file.close()
---
    print("You have been signed up", student)
show_students
# Show the students who signed up for a club
def show_students(club):
---
    print("Students that signed up for the", club, "club:")
---
    file = open(club + ".txt", "r")
---
    for student in file:
---
        student = student.strip()
---
        print(student)
---
    file.close()
menu
# menu choice
def menu():
---
    print("1. Sign up")
---
    print("2. Show students")
---
    choice = ""
    # Only accept input of 1 or 2
    while choice not in ["1", "2"]:
---
        choice = input("Enter choice: ")
---
    # Only accept valid clubs
    print("Clubs available: programming, football or drama")
---
    club = ""
---
    while club not in ["programming", "football", "drama"]:
---
        club = input("Enter the name of the club: ")
---
    # Sign up for the club or output the students in the club
---
    match choice:
        case "1":
            add_student(club)
        case "2":
            show_students(club)