Bbtopher Bbtopher -4 years ago 30
Python Question

Python: Instructor gave me a piece of code with the word "pointer" as a variable

I posted a question earlier today that was downvoted heavily. Long story short you all did me a favor because I worked through it and figured it out myself (thank you). With that being said I'm still confused a bit on how it worked. I'd really like to have a 100% understanding of why my code is working.

My instructor gave the class a piece of code to give us some lead into the assignment.

def lookPhone(lName,fName,phone):
name = input("Please enter the phone number you would like to search for: ")
pointer = 0
if name in phone:
while True:
try:
pointer = phone.index(name,pointer)
print(lName[pointer].title(),fName[pointer].title())
print(phone[pointer])
pointer += 1
except:
break
else:
print("No entry found for " + name)


The part of the code I don't get is the pointer portion. I found out there are no pointers in Python as there are in C++. Why did he define the variable pointer as 0? And why did he redefine it in the boolean portion with the index? I was able to figure out the rest of the code by swapping the index and variables for the other functions but I'm confused about the logic of it all.

What I understand is that if the variable name is in the list "phone" it passes as true. Because of this the print commences and the first name, last name, and number are printed.

EDIT:

The program is suppose to read data from a .txt file and match it with user input like a phone book directory.

Here is the main function that lookPhone reads from. I just didn't understand the point of defining the variable pointer.

#Main defined
def main():
#File opened
myFile = open('entries.txt','r')
lName = []
fName = []
phone = []
count = 1
for line in myFile:
line = line.strip()
line = line.lower()
if count % 3 == 1:
lName.append(line)
elif count % 3 ==2:
fName.append(line)
else:
phone.append(line)
count += 1
myFile.close()

Answer Source

As said, pointer is some random variable name. It points to a character index in the string.

If I understand the logic of the code, it could also be written like this

def lookPhone(lName,fName,phone):
    number = input("Please enter the phone number you would like to search for: ")

    index = 0  # Start at the beginning of the list

    # Loop while we have data
    while name in phone:

        # Try to print the elements from the lists
        try:
            ''' 
            Find the position of 'name' in 'phone' starting from 'index' position
            throws 'ValueError' if not found
            '''
            index = phone.index(name,index)
            print(lName[index].title(),fName[index].title())
            print(phone[index])
        except ValueError:
            break

        # Move to the next position
        index += 1
    else:
        print("No entry found for " + name)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download