Predeso Predeso - 1 year ago 59
Python Question

Multiple possible options with an if statement in .split()

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?")
problem = input("Enter the problem that you have with your mobile phone.").lower()

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

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

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.



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