-2

I need to create a program on which a user can input an issue they are having with their phone. As the reply could be multiple words and lines, I need to be able to pick up key phrases and words such as "doesn't turn on" or "cracked" from the reply. Everything I have tried so far hasn't worked; not really an expert with programming, only started recently.

psuedo code:

x=input("What is wrong with your phone?")
if "dropped" in x:
    print( #text )

I am using Python v3.

Thank you in advance.

idjaw
  • 25,487
  • 7
  • 64
  • 83
Alphin Philip
  • 1,109
  • 2
  • 8
  • 6
  • 1
    You can use the split() function with the space(whitespace) " " sperator. You don't need argument, split() uses spaces as default separator. – Aeldred Apr 11 '16 at 13:58
  • Welcome to SO. Can you please show what you've tried so far? Do you need a specific approach (e.g. arrays, regexp...)? – Laur Ivan Apr 11 '16 at 13:59
  • If you have a limited and small set of keywords to look up, then BAH's answer below should suffice. – trans1st0r Apr 11 '16 at 14:00
  • What if I have a large set of key words? I think I will be looking to detect about 25 or so words, – Alphin Philip Apr 11 '16 at 20:11
  • Thank you, I'll be a daily visitor if i want to finish this course :P. No I haven't used any arrays and am not too sure what an ""regexp" is. I think i'll try BAH's way and see if it works. Laur – Alphin Philip Apr 11 '16 at 20:15

3 Answers3

4

Here's an approach:

x = input("What is wrong with your phone?")
keywords = ["doesn't turn on", "cracked", "dropped"]
if any(keyword in x for keyword in keywords):
    print("test")
Bahrom
  • 4,752
  • 32
  • 41
2

You can use split() and then in

For example:

response = input("What is wrong with your phone?")

responseList = response.split()

if "dropped" in responseList:
    #print( #text )
Ganesh Kathiresan
  • 2,068
  • 2
  • 22
  • 33
1

There is nothing wrong with the code you have already entered.

x = input("What is wrong with your phone?")
if "dropped" in x:
   print("You've dropped your phone!")
Trev Davies
  • 391
  • 1
  • 10