1

Sorry for the poor explanation in the question, but here's my code:

import csv

file = open("problems_solutions.csv","a+")
name = input("What is your name?")
file.write(name+",")
problem = input("Enter the problem that you have with your mobile phone.").lower()
file.write(problem+"\n")
file.close()

Basically, it asks the user for their name and the problem that they have and writes it to a .csv; this part works perfectly.

if ["sound","speaker","volume","audio","earphone","earphones","headphones","headphone"] in problem.split():
    file=open("nosound Solutions.txt","rb")
    print(file.read())
    file.close()

if ["battery","charge","charged","low"] in problem.split():
    file = open("lowbattery Solutions.txt","r")
    print(file.read())
    file.close()

However, when the user inputs a problem such as "My sound isn't working", nothing happens after that - no output, nothing. I have tried taking out the square brackets and replacing the commas with ' or ', but since I have multiple of these if statements for different solutions, it instead prints every text document in the code.

The text documents contain the solutions, e.g. "Try restarting the phone.", "Check if it's on mute."

If any other information is required, then I'm more than happy to provide; this problem is frustrating me and I can't find how to fix it.

Thanks

wConnor
  • 13
  • 2
  • You're asking if the whole list of words is in `problem.split()`. change it like `if "battery"or "charge"or"charged" in problem.split():` – sheshkovsky May 27 '16 at 20:58
  • 1
    @sheshkovsky That doesn't do what you think it does. See http://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true – PM 2Ring May 27 '16 at 21:11

6 Answers6

1

You are currently looking for a list within a list. This only looks for literal objects. [1, 2] would be in [3, [1, 2], 9], but not in [1, 2, 3]. The actual concept you are looking for is a set intersection:

>>> problems = {"battery","charge","charged","low"}
>>> user_input = set('the battery is broken'.split())
>>> problems & user_input
{'battery'}

You can then do if problems & user_input: to execute a code block when there is some common ground between the available problems and the user's input.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

I believe the problem is that you are looking for an array in an array... but I think you want to look for a string in the array?

This seemed to work for me:

problem = "My sound isn't working"
for i in ["sound","speaker","volume","audio","earphone","earphones","headphones","headphone"]:
  if i in problem.split():
    file=open("nosound Solutions.txt","rb")
    print(file.read())
    file.close()
    break
0

As currently stated, you ask if whole list is found in another list. Check would be true if problem.split() would be equal to something like [["battery","charge","charged","low"], 'some', 'other', 1, 'values'] (obviously it'll never be).

To do check you want, you may use any builtin function.

if any(word in problem.split() for word in ["battery","charge","charged","low"]):
    pass  # do something
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
0

Your syntax is incorrect. Try this -

arr1 = ["sound", "speaker", "volume", "audio", "earphone", "earphones",
        "headphones", "headphone"]

arr2 = ["battery", "charge", "charged", "low"]

problem = "my battery is not working"

if any(keyword in problem.split() for keyword in arr1):
    print('Problem is related to sound.')

if any(keyword in problem.split() for keyword in arr2):
    print('Problem is related to battery.')
Vedang Mehta
  • 2,214
  • 9
  • 22
0

So you have 2 arrays here, the one with the list of words and the one with the words in the user's problem. You're asking python if your first array is in the second array, which it isn't. Instead, you should iterate through the words and check each to see if it's in the problem.

matching_words = ["sound","speaker","volume","audio","earphone","earphones","headphones","headphone"]
problem_words = problem.split()
problem_found = False

for word in matching_words:
    if word in problem_words:
        problem_found = True
if problem_found:
    file=open("nosound Solutions.txt","rb")
    print(file.read())
    file.close()

And use that as a template for each problem

CamJohnson26
  • 1,119
  • 1
  • 15
  • 40
-2

Like TigerhawkT3 already wrote, you can work with sets. In my opinion this is a very smart way to solve your issue.

Here some example operations:

a = set(["sound","speaker","volume","audio","earphone","earphones","headphones","headphone"])
b = set("My sound isn't working".split())


>>> b         # unique words within your question
set(['sound', 'My', 'working', "isn't"])
>>> b - a     # words within your question but not in your keywords
set(['My', "isn't", 'working'])
>>> b | a     # words in either your keywords or your question
set(['sound', 'earphone', 'working', 'headphones', 'headphone', 'volume', 'speaker', 'earphones', 'audio', 'My', "isn't"])
>>> b & a     # words in both your keywords and your question
set(['sound'])
>>> b ^ a     # words in your keywords or your question but not in both
set(['earphone', 'working', 'headphones', 'headphone', 'volume', 'speaker', "isn't", 'audio', 'My', 'earphones'])

https://docs.python.org/3/tutorial/datastructures.html#sets

no11
  • 62
  • 7
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/12500268) – Luke May 27 '16 at 21:49
  • Thanks for your advise @Luke. I've updated my answer. – no11 May 27 '16 at 22:04
  • If you like an existing answer, you can indicate that by upvoting it, which you will be able to do once you have 15 reputation. This is more useful than posting a new answer that just points to the answer you like. – TigerhawkT3 May 27 '16 at 22:16