简体   繁体   中英

Python 3.X - Search a list using user input and print the result

with open("zipcodes.txt", "r") as ins:
   array = []
   for line in ins:
       array.append(line.split(","))

print(array)

userInput = input("Please enter the codes your looking for: ")

print(array.index(userInput))

First off, this is for a school assignment, so I am not looking for a complete answer, I'm looking for a direction. My assignment is to load in a text file into a list, separate each line. (the text file is every USA zip code, the city the zipcode is for, and the state EX. 93312,BAKERSFIELD,CA). once the list is created the next thing the program needs to do is take in a users zipcode and then print out the zipcode, the city and the state. This is the code I have so far, I am at a total loss of what to do. Again I am not looking for a complete answer, just a direction. Sometimes I miss simple solutions and just need someone to say something that will make it click in my head. Thank you for your time and for any info or help.

array.index() does not return the element itself; It returns at which index it is. For example, if '93267' is the second element in a list and you called array.index('93267'), you'd get '1'. Try creating a variable that holds the index, and then print whatever is at that index:

index1 = array.index(userInput)
print(array[index1))

Yet you may run into an issue with this, as in your code you split each line at a comma- and in your description, your postcode, city and state were separated by a comma. I'm not entirely sure what your original text file looks like, so I could be wrong, but just be aware that it may cause some issues down the track.

Hope this helped; if you run into more issues, feel free to say so. Good luck with your school project!

EDIT: This version doesn't use a dictionary but uses a list instead.

This code is a quick example of one way you could do that. The file zipcodes.txt only has that single example that you gave in the description.

MATCH = """
Zipcode: {}
City:    {}
State:   {}
"""

with open('zipcodes.txt', 'r') as f:
    zipcodes = [line.rstrip().split(',') for line in f]

while True:
    user_zipcode = input("Enter zipcode (or (Q)uit): ")
    if user_zipcode.lower() == 'q':
        break
    for zipcode, city, state in zipcodes:
        if user_zipcode == zipcode:
            print(MATCH.format(zipcode, city, state))
            break
    else:
        print("\nNo match for zipcode: '{}'\n".format(user_zipcode))

Console:

Enter zipcode (or (Q)uit): 12345

No match for zipcode: '12345'

Enter zipcode (or (Q)uit): 93312

Zipcode: 93312
City:    BAKERSFIELD
State:   CA

Enter zipcode (or (Q)uit): q

If I've understood correctly, the structure of array will end up like this:

[['ZIPCODE', 'CITY', 'STATE'], ['ZIPCODE', 'CITY', 'STATE'], ...]

and userInput will hold a string representing a ZIPCODE.

Given that structure of array , for each entry in array (each sublist), the first element in the entry (entry[0]) will be a ZIPCODE.

You could loop through each entry in array , and check if that entry's first element matches the userInput .

If it does, you can print that entry.

Note: To understand why you are getting an error from the print statement, please check this description of list methods , namely the index method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM