Michael Frost Michael Frost - 1 year ago 69
Python Question

display a dictionary value using input

I can import the dictionary successfully and I can get an output from the dictionary of its values but it shows me all the values and not the value that matches the input of the user.

the input is first converted to lower and then split into individual words to be referenced in the dictionary.

# prob_dict = dictionary

# problemlist = input lowercase and split

I have searched many postings but I cannot find a solution that works.

problemlist = problem1.split()

for problem in range(len(prob_dict)):
if prob_dict in problemlist:
solution = []
solution = (prob_dict[problem])
print('Your Solution is:', solution)
print('could not find a solution')

the dictionary

prob_dict = {'wet': ['put in bag of rice to dry out'],
'screen': ['screen will need to be replaced'],
'charger': ['purchase new charger for our store'],

Answer Source

if prob_dict in problemlist is something that will almost never happen. You wouldn't find a dict in a list of strings.

Instead, you should iterate through the items in the list and see if the dictionary contains a key with the item:

problemlist = [p.lower() for p in problem1.split()]

for problem in problemlist:
    if problem in prob_dict:
        print('Your Solution is: ', prob_dict[problem][0])
              #                                        ^ The associated string
        break # remember to break once solution is found
    print('could not find a solution')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download