-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_dictionary.py
More file actions
111 lines (88 loc) · 3.17 KB
/
email_dictionary.py
File metadata and controls
111 lines (88 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import pickle
#Kylie Drawdy
#3/5/2024
#Write a program that keeps names and email addresses in a dictionary as key-value pairs
#The program should display a menu that lets the user look up a persons email address,
#add a new name and email address, change an existing email address, and delete an
#exisiting name and email address. The prgram should pickle the dictionary and save it
#to a file when the user exits the program. Each time the program starts, it should
#retrieve the dictionary from the file and unpickle it.
def main():
#load data from file
try:
#open file in read
with open('dictionary.dat','rb') as inFile:
emailDictionary = pickle.load(inFile)
except FileNotFoundError:
emailDictionary = {}
#Declare variable
optionSelected = 0
while optionSelected != 5:
#Call function to display menu
optionSelected = getMenu()
#if statement to determine selection
if optionSelected == 1:
#Call method to lookup email
emailLookup(emailDictionary)
elif optionSelected == 2:
#Call method to add email
emailAdd(emailDictionary)
elif optionSelected == 3:
#Call method to change email
emailChange(emailDictionary)
elif optionSelected == 4:
#Call method to delete email
emailDelete(emailDictionary)
#Save dictionary to file when user exits
with open('dictionary.dat', 'wb') as outFile:
pickle.dump(emailDictionary, outFile)
def emailDelete(eDictionary):
# Get name
emName = input("Please enter name to delete: ")
# Check to see if employee is there
if emName in eDictionary:
del eDictionary[emName]
else:
print("Name is not in dictionary!")
def emailChange(eDictionary):
#Get name
emName = input("Please enter name: ")
#Check to see if employee is there
if emName in eDictionary:
print("Current email is: ", eDictionary[emName])
#Input email
newEmail = input("Please enter new email: ")
#Reset email
eDictionary[emName] = newEmail
else:
print("Name is not in dictionary!")
def emailAdd(eDictionary):
#Ask user for info
emName = input("Please enter name: ")
emEmail = input("Please enter E-mail: ")
#Verify name is not there
if emName not in eDictionary:
#Enter item in dictionary
eDictionary[emName] = emEmail
else:
print("Name already exists!")
def emailLookup(eDictionary):
#Get user input for name
emailName = input("Enter name to lookup: ")
#Get email for name
emailEmail = eDictionary.get(emailName, '0 - Not Found')
print(emailName, " Email is: ", emailEmail)
def getMenu():
#Print statements to make menu
print("1. Look up email.")
print("2. Add email.")
print("3. Change email.")
print("4. Delete email.")
print("5. End email.")
#Get user input for menu option
selectedOption = int(input("Please enter number of choice: "))
#Input validation
while selectedOption < 1 or selectedOption > 5:
selectedOption = int(input("Enter valid choice: "))
return selectedOption
main()