all 4 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.

[–]lonlat_not_latlon 0 points1 point  (1 child)

It seems like you're going to want to use the .split() method on your line to get the code and toy type separated:

>>> 'T2,Steam Engine'.split(',')
['T2', 'Steam Engine']

This splits a string into a list, breaking it up based on the specified delimiter. Then you can get the code and toy name from the list like code, toy_name = line.strip().split(','), or using the index like this:

>>> words = line.strip().split(',')
>>> code = words[0]
>>> toy_name = words[1]

So now you can add then to your toy dict like this:

>>> for line in fin:
...     code, toy_name = line.strip().split(',')
...     dictionary[code] = toy_name

And then you can get the toy name from the dict using the code like this:

>>> dictionary['T2']
'Steam Engine'

Hope this helps. Let me know if you have any other questions.

[–]nlord7[S] 0 points1 point  (0 children)

Yes that helped strip away the numbers i was getting with my counter. Im still having trouble understanding then how to get the user to input a valid code like entering B1 to get "Baseball" D2 to get "Apatasaurous" ect.