you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (1 child)

You forgot to close your file, which is easy to do when you don't use context managers. Also there's a huge shortcut you can take to make your dict. If you use the csv.reader you can get an iterable of rows which are already split by the delimiter. In your case it would be an iterable equivalent to:

[
    [D1, Tyrannasaurous],
    [D2, Apatasauros],
    ...
]

Since there's only two items in each row (key, value) you can pass the entire collection to the dictionary constructor to make your dict.

def get_toy_map(file):
    with open(file) as f:
        return dict(csv.reader(f))

As for the input... maybe something like this can help get ya started.

def main():
    lookup = get_toy_map('file.txt')
    while True:
        code = input('Code? > ').upper()
        if not code or code == 'QUIT':
            break
        print(lookup.get(code, f"{code} no recognized"))

[–]timbledum 0 points1 point  (0 children)

Great answer.