Newbie here and need some help

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 :slight_smile:

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? :stuck_out_tongue:

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 :slight_smile:

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 :smile:

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?