-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphonebook.py
More file actions
91 lines (64 loc) · 1.55 KB
/
phonebook.py
File metadata and controls
91 lines (64 loc) · 1.55 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
# implementing linear search for names
"""
# A list of names
names = ["Yuliia", "David", "John"]
# Ask for name
name = input("Name: ")
# Search for name
for n in names:
if name == n:
print("Found")
break
else:
print("Not found")
"""
"""
from cs50 import get_string
people = [
{"name": "Yuliia", "number": "+1-617-495-1000"},
{"name": "David", "number": "+1-617-495-1000"},
{"name": "John", "number": "+1-949-468-2750"},
]
# Search for name
name = get_string("Name: ")
for person in people:
if person["name"] == name:
print(f"Found {person['number']}")
break
else:
print("Not found")
"""
"""
# Implements a phone book using a dictionary
from cs50 import get_string
people = {
"Yuliia": "+1-617-495-1000",
"David": "+1-617-495-1000",
"John": "+1-949-468-2750",
} # dict is implemented using curly braces.
# Search for name
name = get_string("Name: ")
if name in people:
print(f"Number: {people[name]}") # we can index into the people dictionary using the value of 'name'
else:
print("Not found")
"""
import csv
name = []
number = []
for i in range(3):
x = input("Names: ")
y = input("Number: ")
name.append(x)
number.append(y)
file = open("phonebook.csv", "a")
"""
name = input("Name: ")
number = input("Number: ")
"""
#writer = csv.writer(file)
writer = csv.DictWriter(file, fieldnames=["name", "number"]) # save the file with fields
#writer.writerow([name,number])
writer.writerow({"name": name, "number": number})
file.close()
print("File saved as phonebook.csv")