1

Intro: I have been continuing to follow a textbook on programming (Python, specifically), with the latest 'problem' to solve, being the creation of a 'Phone Number Look-up'.

The brief: The textbook asks the user to create two parallel arrays (7 friend's names & phone numbers) and a search function that steps through the people array and returns both that search result (based on user input) of the friend(s) name(s) and corresponding phone number (note: names and corresponding phone numbers are using identical index positions.

The issue: I have def a arraysAndSearch module (that has my people and phoneNumbers array) and used enumerate() for ease of looping over the people array and inc. an automatic counter. The program is working, as it's returning full and partial matches of both the person and corresponding index id in the phoneNumbers array, however I have had issues trying to adapt my code, so that case in-sensitivty is observed.

E.g. searching for 'Katie' returns the two expected values from the people array and corresponding index id from phoneNumbers. Searching for 'katie', returns no results, I simply get my error message, that is coded to appear if not arraySearch:.

Code Extract:

def arraysAndSearch(nameSearch):
    people = ['Silviu Ciocanel', 'Katie Trolan', 'Katie MacPherson']
    phoneNumbers = ['310-443-7798', '562-905-3343', '310-983-5209']
    arraySearch = [people for people in enumerate(people) if nameSearch in people[1]]
    for index, nameSearch in arraySearch:
        print("Name: ", nameSearch, "\nPhone:", phoneNumbers[index])
    if not arraySearch:
        print("No matches in phone book :(")
    print("****************************")

Is anyone able to offer some guidance as to how I can integrate case in-sensitivity?

timgeb
  • 76,762
  • 20
  • 123
  • 145
William
  • 191
  • 5
  • 32
  • 1
    "The textbook asks the user to create two parallel arrays" Wow, I hope there is more context to this or else it is horrible advice. The data structure of choice should be a dictionary where the keys are lower-case names and the values are the phone numbers. – timgeb Nov 12 '17 at 17:05
  • Possible duplicate of [How do I do a case-insensitive string comparison?](https://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison) – Kaushik NP Nov 12 '17 at 17:06

1 Answers1

0

An easy way to achieve this is to ensure that both the input and the search string are in the same case (e.g. both are lowercase):

arraySearch = [people for people in enumerate(people) if nameSearch.lower() in people[1].lower()]
zefixlluja
  • 458
  • 5
  • 14
  • 1
    This really helped, thank you. I did try `lower()` however failed to 'bolt it' onto `nameSearch` AND `people`. A lesson learnt in attention to detail. – William Nov 12 '17 at 17:19