NOTE: LANGUAGE IS PYTHON
CS160 Computer Science Lab 17
Objectives
Work with dictionaries Work with functions Work with files
Overview
This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in order of occurrence, for a given month. To do you will need a dictionary that uses the birth date for the key and the name for the value.
To ensure that every gets recognized on their birthday, since someone always brings in treats or cake on a birthday, we will only allow for a single birthday per day. If a specific day is already taken then that person should pick a different date.
Specifics
To get started create a text file using any text editor that will store a series of names and their birthdays. Each line will have a name, a colon separator, and a date. There is no need to store the month, all entries in any given file will be from the same month. An example of a data file, such as “DecemberBirthdays.txt”, might be:
Scott:1 Bob:10 Sue:12 Alex:14 Alice:14 Fred:22
Use the provided code to display a menu. This will accept the user’s choice of what they wish to do within the program and return the integer value associated with the menu choice. The menu options will be opening a file, adding a name, searching by date, displaying all birthdays for the month, and exiting the program. On screen the menu might look like:
What would you like to do? 1. Open a data file
2. Add new names
3. Search by date
4. Display all birthdays for the month 5. Exit program
Your choice?
The menu will allow the user to enter a value between 1 and 5. After each valid entry, perform the specified action and then present the menu again. The primed loop example used for data entry will work well for this as well. The pseudo-code for using the input returned from the menu might be:
choice = showMenu() while choice != 5
if choice == 1:
open file and return a dictionary with the info
elif choice == 2:
allow the user to add new names/birthdays
elif choice == 3: ask for a date and display the associated name
elif choice == 4: display all dates/names for the month
choice = showMenu()
write the dictionary back to the original fileSpecifics for menu
options
Each menu option (1-4) should be implemented in a function.
When reading a data file, ask for the name of a file and then add that information from the file into a dictionary. Ask for the file name in any manner you have done so this year. Ask for the file BEFORE calling the function which reads the data file.
When adding new entries, ask for a name and a date and then add them to the dictionary. If the date entered already has a name, state that the date is taken and that the info will not be added to the dictionary and exit the function. Only add one name/birthday per call of the function – no loop needed in this function.
When searching by date, allow the user to enter the date. Then display the name associated with that specified date. If no one has a birthday on that date display a message stating so. Continue to perform searches until the user enters a date of 0. You will need a loop in this function.
Finally, when the user chooses to exit the program write all the information from the dictionary out to the filename specified earlier in the program. Write out one entry per person. Don’t ask for a new file name, use the file name the user provided when opening the file.
An example of running the program might be something like the following. The program output is in blue. This is not actual output. And remember that the dates will not necessarily be in order. We’ve discussed in class how to print a dictionary in sorted order. Take that as a challenge, but NOT A REQUIREMENT for the lab. If you want to have a little more fun, offer the choice of printing out the information sorted by name or date. Again, not a requirement, but an interesting challenge.
What would you like to do? 1. Open a data file
2. Add new names
3. Search by date
4. Display all birthdays for the month 5. Exit program
Your choice? 1
<<user selects a file using input() or FileUtils.selectOpenFile ()>>
What would you like to do? 1. Open a data file
2. Add new names
3. Search by date
4. Display all birthdays for the month 5. Exit program
Your choice? 4
Tom 1 Bob 10 Scott 20
What would you like to do? 1. Open a data file
2. Add new names
3. Search by date
4. Display all birthdays for the month 5. Exit program
Your choice? 2 Name to add: Angie
Date for Angie: 30
Angie has been added to the birthday list
What would you like to do? 1. Open a data file
2. Add new names
3. Search by date
4. Display all birthdays for the month 5. Exit program
Your choice? 4
Tom 1 Bob 10 Scott 20 Angie 30
CODE:
FILENAME = ""
def showMenu():
print()
print('Menu')
print('------------------------------')
print('1. Open a data file ')
print('2. Add new names ')
print('3. Search by date')
print('4. Display all birthdays for the month ')
print('5. Exit program')
print()
choice = int(input('Enter the choice: '))
return choice
def openFile():
global FILENAME
FILENAME = input('Enter the name of the file: ')
file = open(FILENAME,'r')
data = file.readlines()
dic={}
for i in data:
i = i.rstrip()
l = i.split(":")
if l[1] in dic.keys():
print("Entry",i,"exists , Hence ignored ")
else:
dic[l[1]] = l[0]
file.close()
return dic
def addNew(dic):
name = input("Name to Add: ")
msg = "Date for "+name+": "
date = input(msg)
if date in dic.keys():
print("Entry for",date,"exists , Hence ignored ")
else:
dic[date] = name
print(name,"has been added to the birthday list")
return dic
def search(dic):
date = input("Enter the date:")
if date in dic.keys():
print(date,":",dic[date])
else:
print("No Entry Found")
def display(dic):
for key in sorted(dic.keys()):
print(key,":",dic[key])
#Called when user inputs 5
def savFile(dic):
file = open(FILENAME,'w')
for key in sorted(dic.keys()):
file.write(dic[key]+":"+key+"\n")
file.close()
#Main function
def main():
choice=0
info={}
while True:
choice = showMenu()
if choice == 1:
info = openFile()
elif choice == 2:
info = addNew(info)
elif choice == 3:
search(info)
elif choice == 4:
display(info)
elif choice == 5:
savFile(info)
return
main()
Sure! Below is a Python implementation of the program based on the provided specifications. Please note that the implementation assumes Python 3.x:
pythonCopy codeimport osdef showMenu(): print("What would you like to do?") print("1. Open a data file") print("2. Add new names") print("3. Search by date") print("4. Display all birthdays for the month") print("5. Exit program") return int(input("Your choice? "))def readDataFile(filename): birthday_dict = {} with open(filename, 'r') as file: for line in file: name, date = line.strip().split(':') birthday_dict[int(date)] = name return birthday_dictdef writeDataFile(filename, birthday_dict): with open(filename, 'w') as file: for date, name in birthday_dict.items(): file.write(f"{name}:{date}\n")def addNewNames(birthday_dict): name = input("Name to add: ") date = int(input("Date for {}: ".format(name))) if date in birthday_dict: print("Date is already taken. Entry not added.") else: birthday_dict[date] = name print("{} has been added to the birthday list.".format(name))def searchByDate(birthday_dict): date = int(input("Enter the date you want to search for (0 to exit): ")) while date != 0: if date in birthday_dict: print("Name associated with {} is {}".format(date, birthday_dict[date])) else: print("No one has a birthday on {}".format(date)) date = int(input("Enter the date you want to search for (0 to exit): "))def displayAllBirthdays(birthday_dict): for date, name in sorted(birthday_dict.items()): print("{} {}".format(name, date))def main(): birthday_dict = {} filename = None choice = showMenu() while choice != 5: if choice == 1: filename = input("Enter the filename of the data file: ") birthday_dict = readDataFile(filename) elif choice == 2: addNewNames(birthday_dict) elif choice == 3: searchByDate(birthday_dict) elif choice == 4: displayAllBirthdays(birthday_dict) else: print("Invalid choice. Please try again.") choice = showMenu() if filename: writeDataFile(filename, birthday_dict) print("Data has been saved to {}".format(filename))if __name__ == "__main__": main()
Please make sure you have a data file in the same directory as the Python script. The data file should have the format as mentioned in the lab instructions. When you run the program, it will display the menu, and you can select the desired options to open a file, add new names, search by date, display all birthdays, and exit the program. The data will be saved to the file specified when you exit the program.
NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions...
NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...
In this project, you will use functions and dictionaries to track basketball players and their respective points, then display statistics of points made. You will need three functions as follows: def freeThrowMade(playerDictionary, playerName) - this function will add 1 point to the player's total score def twoPointMade(playerDictionary, playerName) - this function will add 2 points to the player's total score def threePointMade(playerDictionary, playerName) - this function will add 3 points to the player's total score Each of these functions has...
C++ : Please include complete source code in answer This program will have names and addresses saved in a linked list. In addition, a birthday and anniversary date will be saved with each record. When the program is run, it will search for a birthday or an anniversary using the current date to compare with the saved date. It will then generate the appropriate card message. Because this will be an interactive system, your program should begin by displaying a...
Lab 4: Databased Admin Tool Continued Python Objective: In the previous lab, you developed an admin tool for username and password management. You accomplished the tasks with Dictionary or List. Here, use class to design your own data structure to accomplish a similar task. The UD.txt datafile: FIRST NAME, LAST NAME,USERNAME,PASSWORD Sam, DDal,sdd233,Pad231 Dave, Dcon,dcf987, BHYW4fw Dell, Grant,dgr803,Sb83d2d Mike, Kress,mkr212,UNNHS322 Lisa,Kate,lki065,dgw6234 Paul,Edward,ped332,9891ds Youyou,Tranten,ytr876,dsid21kk Nomi,Mhanken,nmh223,3282jd3d2 Write a program that imports the database from UD.txt, your program can search for users’ password....
C++ program Write a C++ program to manage a hospital system, the system mainly uses file handling to perform basic operations like how to add, edit, search, and delete record. Write the following functions: 1. hospital_menu: This function will display the following menu and prompt the user to enter her/his option. The system will keep showing the menu repeatedly until the user selects option 'e' from the menu. Arkansas Children Hospital a. Add new patient record b. Search record c....
Objectives This is one of three major programming projects this semester. You should NOT collaborate on this project. While you may ask for assistance in debugging, this project should be ENTIRELY your own work. Objectives include: . Use local variables. • Use arithmetic expressions. • Use Scanner to input values. • Use a class constant. • Use of nested branches. • Use for loxops. • Use of methods. • Use of arrays. Hand-in Requirements All projects and laboratories will be...
The purpose of this assignment is to get experience with an array, do while loop and read and write file operations. Your goal is to create a program that reads the exam.txt file with 10 scores. After that, the user can select from a 4 choice menu that handles the user’s choices as described in the details below. The program should display the menu until the user selects the menu option quit. The project requirements: It is an important part...
Chapter 8 Python Case study Baseball Team Manager For this case study, you’ll use the programming skills that you learn in Murach’s Python Programming to develop a program that helps a person manage a baseball team. This program stores the data for each player on the team, and it also lets the manager set a starting lineup for each game. After you read chapter 2, you can develop a simple program that calculates the batting average for a player. Then,...
write a ContactBook in C++ ContactBook that holds 10 names with that names corresponding contact (last name and first name of the owner). Ask the user to input up to 10 Contacts (It may be less. The user should have the ability to stop inputting Contacts whenever he wishes). 3.This class will store a list of Contacts in a stack allocated array of default capacity 10. 4.You must be able to add new contacts to a list, delete old contacts,...
CSCI 3000 Homework 4In this assignment, you will implement a simple version of Computer Lab administration system in C++. Your program will monitor computers in 4 computer labs and will allow users to log in and log out. Each computer lab has different number of computers.· Lab 1 has 10 computers· Lab 2 has 6 computers· Lab 3 has 3 computers· Lab 4 has 12 computersHere is a sample state of the system:Lab ArraySome of the computers are free (no...