My program is not producing an output, why is this?

Viewed 42

I recently just asked a question on SO regarding on how to turn a text file into a dictionary on Python, it worked until I made a slight adjustment to the code as I want to compare the key to a word in a string, and if the key was the same as the word, it would print the corresponding value, however no output is printed, why is this?

def answerInput():
    print("Hello, how may I help you with your mobile device")
    a = input("Please enter your query below\n")
    return a

def solution(answer):
    string = answer
    my_dict = {}
    with open("Dictionary.txt") as f:
        for line in f:
            key, value = line.strip("\n").split(maxsplit=1)
            my_dict[key] = value
            for word in string.split():
                if word == my_dict[key]:
                    print(value)
            
process = 0
answer = answerInput()
solution(answer)

If anyone it helps, my text file is as goes:

battery Have you tried charging your phone? You may need to replace your battery.
dead Have you tried charging your phone? You may need to replace your battery.
sound Your volume may be on 0 or your speakers may be broken, try using earphones.
screen Your screen may be dead, you may need a replacement.
touchID You may need to wipe the fingerprint scanner, try again.
microphone You may need to replace your microphone.
3 Answers

you're trying to compare the variable 'answer' to the variable 'key', not the value stored in my_dict[key]. also, I'd suggest you use .lower() such as: if answer.lower() == key so you'd be able to process user responses even if they are in upper case letters

Read the file into my_dict dictionary first, then do lookups into it (your current code as posted reads the file as it does the lookups). Also, compare the words of the user's answer to the keys, not values of the my_dict dictionary (as your code does).

All of this also means that the code should have 3 parts, perhaps something similar to the solution below.

def read_dictionary(in_file):
    my_dict = {}
    with open(in_file) as f:
        for line in f:
            key, value = line.strip('\n').split(maxsplit=1)
            my_dict[key] = value
    return my_dict
    
def read_query():
    print('Hello, how may I help you with your mobile device')
    return input('Please enter your query below\n').strip('\n')

def solve(query, my_dict):
    for word in query.split():
        if word in my_dict:
            print(my_dict[word])
            return
    return

my_dict = read_dictionary('Dictionary.txt')
query = read_query()
solve(query, my_dict)

I'm not sure if this is the way you want it to work and configparser usage is a little different but I kinda like the way it can work here.

import configparser


config = configparser.ConfigParser()
config.read("conf.ini", encoding="utf-8")


def answerInput():
    print("Hello, how may I help you with your mobile device")
    a = input("Please enter your query below\n")
    return a


answer = answerInput()
print(config.get("Answers", answer))

conf.ini file in the same directory:

[Answers]
battery = Have you tried charging your phone? You may need to replace your battery.
dead = Have you tried charging your phone? You may need to replace your battery.
sound = Your volume may be on 0 or your speakers may be broken, try using earphones.
screen = Your screen may be dead, you may need a replacement.
touchID = You may need to wipe the fingerprint scanner, try again.
microphone = You may need to replace your microphone.

This is obviously depending if you already have a huge list of those key value pairs or you are gonna be creating ones. If you are creating ones, this might be a nice strucured way to do it.

Related