I am currently in a Python class (my first time ever with Python), and i have a hoework assignment that i am having trouble with. Here is the assignment:
Write a program that queries information from three files. The first file (data1.txt) contains the names and telephone numbers of a group of people. The second file (data2.txt) contains the names and Social Security numbers of a group of people. The third file (data3.txt) contains the Social Security numbers and annual incomes of a group of people. The groups of people should overlap but need not be completely identical. Your program should ask the use for a telephone number and then print the name, Social Security number, and annual income, if it can determine that information.
Data1.txt:
Bob, 555-1234
Matthew, 555-2345
Mark, 555-3456
Luke, 000-1234
John, 000-2345
Data2.txt
Matthew, 000000001
Mark, 000000002
Luke, 000000003
Joe, 000000004
Data3.txt
000000001, 55000
000000003, 65000
000000004, 70000
000000007, 10000000
Here is what I have coded thus far, but i am truly stuck:
dataFile = open("data1.txt", "r")
dataFile2 = open("data2.txt", "r")
dataFile3 = open("data3.txt", "r")
for line in dataFile:
x = line.split(",")
for line in dataFile2:
x = line.split(",")
for line in dataFile3:
x = line.split(",")
print ("Enter the phone number (7 digits, with a dash): ")
userInput = input("")
if userInput == dataFile(x[1]):
For now, start by making 3 dictionaries,
- one which has telephone numbers as keys and names as values,
- one which has names as keys and social security numbers as values,
- one which has social security numbers as keys and annual incomes as values
Then, you can ask the user for input and search through the dictionaries
1 Like
Thanks! That makes senseâŚone question (and please forgive my ignorance), is how do you search via all the dictionaries? Nested If statements?
roughly something like:
name = dict1.get(telephone_number, None)
number = None # just to have them defined
income = None # just to have them defined
if name:
number = dict2.get(name, None)
if number:
income = dict3.get(number, None)
# do something with name, number, income
1 Like
Ok, I gotchaâŚi will give this a try and let you know! Thanks a bunch!
i thought i got it but i am beyond confused at this point! How do you tie the dictionaries together? If a user inputs a ph number, then it is supposed to give the user the contactâs name, ssn, and telephone numberâŚi am just not understanding how to tie them together.
Sorry for slow reply, I was asleep when you posted and just finished work now
You donât need to âtie them togetherâ. I mean you can if you want, but thatâs probably more work
If you look at the code Iâve posted, you can first use the phone number to get the name, then if you managed to get the name you can use the name to get the SSN, then if you managed to get the SSN you can get the account balance
So you can just use the 3 dictionaries as they are
1 Like
In your code, when you specify âNoneâ what do you mean by this?
1 Like
Itâs just a special value in Python that represents the lack of value
If you tried using the expression:
if number:
and number
hadnât been created before that point, youâd get an error, probably NameError
if I recall correctly. None
is falsy, so itâd never succeed the if number
with an if None
Edit: I should probably point out that you donât need to add None
in the get
calls (the second argument is what value to return if what youâre looking for isnât in the dictionary), as itâs the default returned value, I just like being explicit
So what am i missing here?
d1 = {}
d2 = {}
d3 = {}
data1 = open("data1.txt", "r")
data2 = open("data2.txt", "r")
data3 = open("data3.txt", "r")
for line in data1:
x = line.split(",")
a = x[0]
b = x[1]
b.rstrip()
d1[a] = b
for line in data2:
x = line.split(",")
a = x[0]
b = x[1]
b.rstrip()
d2[a] = b
for line in data3:
x = line.split(",")
a = x[0]
b = x[1]
b.rstrip()
d3[a] = b
number = d1.get(None, 1)
social = d2.get(0, None) # just to have them defined
income = d3.get(0, None) # just to have them defined
print("Enter the phone number (7 digits, with a dash): ")
userInput = input("")
while userInput == number:
if number:
name = d1.get(None, 1)
if social:
income = d3.get(0, None)
print(number, "is associated with", name)
print(name,"'","SSN is",social)
print(name,"'","Salary is $",income)
else :
print(number, "is associated with", name)
print("Couldn't find a SSN for", name)
else:
print("Couldn't find a name associated with that number.")
get
( key [, default ])
Return the value for key if key is in the dictionary, else default . If default is not given, it defaults to None
, so that this method never raises a KeyError
.
The first argument to get
is the key you want to look up in the dictionary
In this case, you get a phone number from the user, and should then look it up in d1
Note that the data in data1
actually gives the name as the second value
What if there are multiple keys? For example, when a user enters a ph number, that number has to correlate to a name in data1.txt. Well, my âgetâ needs to look for multiple keys right? But name is a key, so how do I print a key for the message? So confusing, but I truly appreciate your help!
Again was asleep,
What do you mean by multiple keys? Dict one only has phone numbers as keys, and names as values
Dict two has names as keys and ssns as values, etc
Dict one has the names as keys and numbers as values. So my confusion is, if a user has to input a number, then how do i get the name and then how is all of that tied to one another?
When you make the dictionary why donât you make the numbers the keys?
Oh waitâŚyou can do that?!? How?
How do you usually add key value pairs? Put them in dict1 in the opposite order to how youâre doing now
hahahaâŚgeez! Ok, so when you call for a key is it indexed? For example, if the user inputs the telephone number, how to access the value? Would it be number = d1.get(userInput, None)?
User input is a phone number, so thatâd return a name
You should try playing around with putting items in a dictionary in the python prompt, youâll get the hang of them in no time
yeahâŚbut thatâs what i am trying to get is a name. So for instance:
print("Enter the phone number (7 digits, with a dash): ")
userInput = input("")
So when a user inputs the number (i.e. 555-1234), how do i populate the number variable in your example?