Skip to content

Commit e41875f

Browse files
author
Jym Patel
authored
Create contacts.py
1 parent 03ad3ba commit e41875f

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

ultimate-phone-book/contacts.py

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# code by @JymPatel
2+
# edited by @bupboi1337, (editors can put their name here && thanks for contribution :)
3+
4+
# this code uses GPL V3 LICENSE
5+
## check license it https://github.com/JymPatel/Python-FirstEdition/blob/Main/LICENSE
6+
print("this code uses GPL V3 LICENSE")
7+
print("")
8+
9+
# start of code
10+
# import library
11+
import pickle
12+
import os
13+
14+
# get array from pickle data
15+
infile = open('data/pickle-main', 'rb')
16+
# defining array
17+
array = pickle.load(infile)
18+
infile.close()
19+
20+
# get key if path exists
21+
keyacess = False
22+
path = 'data/pickle-key'
23+
if os.path.isfile('data/pickle-key'):
24+
pklekey = open('data/pickle-key', 'rb')
25+
key = pickle.load(pklekey)
26+
pklekey.close()
27+
if key == 'SKD0DW99SAMXI19#DJI9':
28+
keyacess = True
29+
print("key found & is correct")
30+
print("ALL FEATURES ENABLED")
31+
else:
32+
print("key is WRONG\nSOME FEATURES ARE DISABLED")
33+
print("check https://github.com/JymPatel/Python-FirstEdition/tree/Main/PyPrograms/contacts for key, it's free")
34+
print("key isn't added to this repo check above repo")
35+
else:
36+
print("key not found\nSOME FEATURES ARE DISABLED")
37+
print("check https://github.com/JymPatel/Python-FirstEdition/tree/Main/PyPrograms/contacts for key, it's free")
38+
39+
print("")
40+
print("update-22.02 ADDS SAVING YOUR DATA WHEN CLOSED BY SAVING USING OPTION 0\n##")
41+
42+
# for ease in reading
43+
fname = 0
44+
lname = 1
45+
number = 2
46+
email = 3
47+
# getting some variables
48+
promptvar = 0 # variable for prompt
49+
loopvar = 0 # variable for main loop
50+
# making loop to run
51+
while loopvar < 1:
52+
# ask user what to do
53+
print("") # putting blank line before running new loop
54+
if promptvar == 0:
55+
print("0. exit program")
56+
print("1. get all contacts")
57+
print("2. add new contact")
58+
print("3. remove any contact")
59+
print("4. sort contacts by first name")
60+
print("9. stop getting this prompt")
61+
62+
a = input("WHAT WOULD YOU LIKE TO DO? ")
63+
64+
# check for integer & calculate length of array
65+
try:
66+
a = int(a)
67+
except ValueError:
68+
print("!! PLEASE ENTER AN INTEGRAL VALUE")
69+
# get length of array
70+
arraylen = len(array[fname])
71+
72+
# if option 1 is selected
73+
if a == 1:
74+
print("")
75+
print("== YOUR CONTACT LIST ==")
76+
print("")
77+
i1 = 0
78+
# print all names
79+
while i1 < arraylen:
80+
print(f"{array[fname][i1]} {array[lname][i1]}, {array[number][i1]} {array[email][i1]}")
81+
i1 += 1
82+
print("=======================")
83+
84+
# option 2 is selected
85+
elif a == 2:
86+
# get a new contact
87+
array[fname].append(input("First Name: "))
88+
array[lname].append(input("Last Name: "))
89+
array[number].append(input("Phone Number: "))
90+
array[email].append(input("email ID: "))
91+
arraylen += 1
92+
93+
# option 3
94+
elif a == 3:
95+
print("which contact would you like to delete? (enter first name)")
96+
print("enter '\nSTOP' to STOP deleting contact")
97+
rmcontact = input("INPUT: ")
98+
if rmcontact != '\nSTOP':
99+
tempvar = 0
100+
rmvar = 0
101+
for i in range(arraylen):
102+
if array[fname][i].upper() == rmcontact.upper():
103+
tempvar += 1
104+
rmvar = i
105+
# if no cotacts found
106+
if tempvar == 0:
107+
print("no cantact matches first name provided")
108+
# if only one contact is found
109+
elif tempvar == 1:
110+
print("DO YOU WANT TO DELETE CONTACT")
111+
for i in range(4):
112+
print(array[i][rmvar])
113+
tempinp = input("y/n? ")
114+
if tempinp == 'y' or tempinp == 'Y':
115+
for i in range(4):
116+
del array[i][rmvar]
117+
print("contact REMOVED.")
118+
else:
119+
print("failed to REMOVE contact")
120+
# if more than one contact is found
121+
else:
122+
print("there are more than one contact with same name")
123+
# TODO
124+
125+
126+
127+
# if option 4 is selected
128+
elif a == 4:
129+
if keyacess == True:
130+
sortcounter = 1
131+
while sortcounter != 0:
132+
# reset counter
133+
sortcounter = 0
134+
arraylen = len(array[fname])
135+
for i in range(arraylen - 1):
136+
if array[fname][i].upper() > array[fname][i + 1].upper():
137+
for j in range(4):
138+
temp = array[j][i]
139+
array[j][i] = array[j][i + 1]
140+
array[j][i + 1] = temp
141+
# add one for changing values
142+
sortcounter += 1
143+
if array[fname][i].upper() == array[fname][i + 1].upper():
144+
# if first name are same, compare last
145+
if array[lname][i].upper() > array[lname][i + 1].upper():
146+
for j in range(4):
147+
temp = array[j][i]
148+
array[j][i] = array[j][i + 1]
149+
array[j][i + 1] = temp
150+
# add one for changing values
151+
sortcounter += 1
152+
# if no values are swapped, sortcounter = 0; no next loop
153+
print("CONTACTS ARE NOW SORTED")
154+
else:
155+
print("NEED CORRECT KEY TO ENABLE THIS FEATURE")
156+
157+
# option 9
158+
elif a == 9:
159+
if keyacess:
160+
# change prompt settings
161+
if promptvar == 0:
162+
promptvar += 1
163+
print("you won't get prompt now!")
164+
print("ENTER 9 AGAIN TO START GETTING PROMPT AGAIN!!")
165+
else:
166+
promptvar -= 1
167+
else:
168+
print("NEED CORRECT KEY TO ENABLE THIS FEATURE")
169+
170+
171+
# if option 0 is selected
172+
elif a == 0:
173+
print("Saving your Data ...")
174+
outfile = open('data/pickle-main', 'wb')
175+
pickle.dump(array, outfile)
176+
outfile.close()
177+
print("YOUR DATA HAS BEEN SAVED SUCESSFULLY!")
178+
loopvar += 1
179+
180+
# if no true option is selected
181+
else:
182+
print("!! PLEASE ENTER VALUE FROM GIVEN INTEGER")
183+
184+
# end of code
185+
print("")
186+
print("get this code at https://github.com/JymPatel/Python-FirstEdition")

0 commit comments

Comments
 (0)