如何从文本文件导入特定解决方案以响应用户? [英] How do i import specific solutions from textfiles to respond to the user with?

查看:50
本文介绍了如何从文本文件导入特定解决方案以响应用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中:

in my code:

import sys #imports sys module to use in python
key_words = ("freezing", "crashing", "restarting","apps")
key_words1=("blank", "white", "frozen", "blurry","dropped")
key_words2=("wet", "water", "soaked", "drenched", "moisture")
key_words3=("cracked", "smashed", "crack")
key_words4=("connection","wifi","cellular"
choice=("yes")
choice1=("no")
    user=input("Are you having any problems with your phone?")
    user=(user.lower())
    if user!=choice and user !=choice1:
        print("Sorry, I didn't understand that, make sure to reply with 'YES' or 'NO'.")
        continue
    if user==choice1:
        print("that's great!")
        sys.exit(0) #ends program if user's phone is working
    elif user==choice:
        break 

user_input = input("what problems are you having with your phone? ")
user_input=(user_input.lower())

if any(i in key_words for i in user_input.split()):
    print("Check for system updates")#advice for user to fix their phone
elif any(i in key_words1 for i in user_input.split()):
    print("Force your phone to restart ")
elif any(i in key_words2 for i in user_input.split()):
    print("1.Fill a container with rice \n 2.Put your phone inside the container for 24-48 hours until dry")
elif any(i in key_words3 for i in user_input.split()):
    print("Get it replaced by yourself, a local screen-fixing shop or the supplier of your phone")   
elif any(i in key_words4 for i in user_input.split())
    print("restart both your router and your phone.")





我不得不打印建议,我必须让python打开一个文本文件,其中Python打印出解决方案取决于用户的问题。这是我的文本文件:

检查系统更新

强制你的手机重启

用米饭填充容器

将手机放入容器内

由您自己,当地的屏幕固定商店或供应商更换

重新启动路由器和手机



我理解如何清洁和打开纺织品,但我希望它根据用户的输入选择解决方案。因此,如果用户说我的手机已经掉线,它会从我创建的文本文件中打印出强制重启手机



我是什么尝试过:



我试过打开我的文本文件,该文件有效,但是我不知道怎么会把它分开,所以我可以只响应我的文本文件中的一行或更多依赖于用户输入的内容



Instead of having to print the advice, I have to get python to open a text file, where Python prints out solutions from it depending on the user's problem. This is my textfile:
check for system updates
Force your phone to restart
Fill a container with rice
Put your phone inside the container
Get it replaced by yourself, a local screen-fixing shop or the supplier
restart both your router and your phone

I understand how to clean and open the textile but how I want it pick solutions out dependant from what the user inputs. So if the user says" my phone has dropped", it prints out "Force your phone to restart" from the text-file I created

What I have tried:

I have tried opening my text-file, which works and readlines but I'm not sure how i would split it up so I can respond with only one of the lines from my text-file or more dependant on what the user inputs

推荐答案

您可以按11.2。 fileinput - 迭代多个输入流中的行 [ ^ ]。当你重新划线时,你应该把它们存放在某种类型的容器中。然后,您可以使用某种形式的关键字搜索来匹配用户输入和解决方案。但是你还需要考虑一下用户输入我的手机不会重启的情况。
You can read each line as described in 11.2. fileinput — Iterate over lines from multiple input streams[^]. As you reade the lines you should store them in a container of some type. You can then use some form of keyword search to match user input with solutions. But you also need to think about a situation such as the user entering "my phone will not restart".


这篇关于如何从文本文件导入特定解决方案以响应用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆