Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.")


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
Posted
Updated 6-Mar-16 1:57am

1 solution

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".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900