Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
111 views
in Technique[技术] by (71.8m points)

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

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
")
    return a

def solution(answer):
    string = answer
    my_dict = {}
    with open("Dictionary.txt") as f:
        for line in f:
            key, value = line.strip("
").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.
question from:https://stackoverflow.com/questions/65545903/my-program-is-not-producing-an-output-why-is-this

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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('
').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
').strip('
')

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...