13.3 - Even Years
Problem
A program is required to work out who was born in even years.
Write a program that will read the data of 10 users from data.txt (name, dob) into an array of records. The program should then display the names of all users born in an even year.
Example Run
John Smith Michael Brown Daniel Wilson James Anderson William Jackson
John Smith,12/03/2008 Emily Jones,25/07/2007 Michael Brown,18/11/2008 Sarah White,02/05/2007 Daniel Wilson,30/09/2008 Olivia Taylor,14/01/2007 James Anderson,22/06/2008 Sophia Thomas,08/12/2007 William Jackson,17/04/2008 Ava Harris,03/10/2007
| 1 | Read data into array of records | IN | |
| OUT | users(name, dob) | ||
| 2 | Find and display all users born in an even year | IN | users(name, dob) |
| OUT |
2.1 for i from 0 to length of users - 1 2.2 set year to characters 6 to 9 of users[i].dob 2.3 convert year to integer 2.4 if year MOD 2 = 0 then 2.5 display users[i].name 2.6 end if 2.7 end for
# Main users = read_data() display_even_year(users)
from dataclasses import dataclass
@dataclass
class User:
name: str = ""
dob: str = "" # Format: DD/MM/YYYY
def read_data():
users = [User() for i in range(10)]
file = open("data.txt", "r")
for i in range(10):
users[i].name, users[i].dob = file.readline().strip().split(",")
file.close()
return users
def display_even_year(users):
for i in range(len(users)):
year = int(users[i].dob[6:10])
if year % 2 == 0:
print(users[i].name)
# Main
users = read_data()
display_even_year(users)
from dataclasses import dataclass
# Define a record structure to store user details
@dataclass
class User:
name: str = ""
dob: str = "" # Format: DD/MM/YYYY
# Function to read data from file into an array of records
def read_data():
# Create an array of records with 10 empty entries
users = [User() for i in range(10)]
# Open the file for reading
file = open("data.txt", "r")
# Read each line and store values in the array of records
for i in range(10):
# Read line, remove whitespace, split into name and dob
users[i].name, users[i].dob = file.readline().strip().split(",")
# Close the file
file.close()
# Return the array of records
return users
# Function to display names of users born in even years
def display_even_year(users):
# Loop through each record in the array
for i in range(len(users)):
# Extract year from date of birth
year = int(users[i].dob[6:10])
# Check if year is even
if year % 2 == 0:
# Display the user's name
print(users[i].name)
# Main program
# Read data into array of records
users = read_data()
# Display users born in even years
display_even_year(users)
Extension 1
Create a second procedure to find all of the people born in odd years.
Extension 2
Create a procedure that allows the user to search for a specific year, then return all of the names of people who are born in that year.
Extension 3
Create a procedure that allows the user to enter a year.
It should find all people that WAS NOT born in that year, and write the names of those people to a file.
Extension 4
Create a procedure that allows a user to add new people to data.txt.
It should validate their name and their birth date to ensure the format is the same.