Python使用字典来检查查询和检索ans

2024-10-03 02:47:59 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,根据我的守则:

ValidInputs = ['wet', 'water', 'liquid', 'mobile', 'iphone']
root = Tk()
def iphone():
   while True:
     Problem = input('State your problem')
     ProblemSplit = Problem.split(' ')
     if any(Words in ValidInputs for Words in ProblemSplit):
        print('Put your phone in the fridge for about 30 minutes, it will remove the moisture inside of your phone')
        time.sleep(2)

这是可行的,但我认为效率低下。举例来说,如果我使用Problem变量的输入,并输入'my phone is wet',它将检测到wet确实在ValidInputs列表中,但是每次它都会打印相同的答案/解决方案。因此,如果我在ValidInputs列表中添加了更多可接受的条目,我不希望它总是打印相同的“将手机放入冰箱”等。在

那么,我该如何创建一个具有以下格式的词典, 查询:解决方案 这样,如果问题分割中有任何单词出现在查询中,则打印解决方案。 有什么建议吗?

我已经尝试了一个小时了,但是没有运气,我已经很接近了,但是到目前为止,如果你明白我的意思,通过我自己的研究,但是它变得很无聊,所以

感谢任何帮助。在


Tags: thein列表foryourphone解决方案words
1条回答
网友
1楼 · 发布于 2024-10-03 02:47:59

这里有一个最小的例子,我相信你在寻找什么。在

solutions = {'wet' : 'put into microwave', 'ring' : 'hit with hammer', 'battery' : 'more jiggawatts'}

def find_solution():
    problems = input('State your problem: ').lower().split(' ')
    for problem in set(problems):
        if problem in solutions:
            print(solutions[problem])

while True:
    find_solution()

相关问题 更多 >