1

So, basically, this is my code:

import random
import os

answer = input('What is the problem with your mobile phone? Please do not enter more than one sentence.')
print('The program has detected that you have entered a query regarding: ' 
if 'wet' or 'water' or 'liquid' or 'mobile' in answer:
    print('Put your mobile phone inside of a fridge, it sounds stupid but it should work!')

What I want to know is, say for example if the user enters the keywords 'wet' and 'mobile' as their input, how do I feed back to them knowing that my program has recognised their query.

So by saying something like 'The program has detected that you have entered a query regarding:' how do I filter their keywords into this sentence, say, if they entered 'My mobile phone has gotten wet recently', I want to pick out 'mobile' and 'wet' without saying:

print('The program has detected that you have entered wet')

Because that sounds stupid IMO.

Thanks

4
  • Put the print statement inside your if test. Actually, your entire if statement is invalid an needs to be re-coded. Commented Nov 20, 2015 at 18:36
  • Yeah, and I knew it should have been an if not an elif and I need to remove import os as well, I was just trying something out before hence why there is un needed bits of code Commented Nov 20, 2015 at 18:38
  • But I want a way to pull specific strings from the users input, for example say if there was a 'valid inputs' list and a couple of the strings from the users input matched the list, then it would print said strings Commented Nov 20, 2015 at 18:39
  • How do you suggest I code it then? Commented Nov 20, 2015 at 18:43

2 Answers 2

2

If I understand your question correctly, this should solve your problem. Just put the print statement inside the if condition! Very simple, I guess :)

import random
import os

answer = input('What is the problem with your mobile phone? Please do not enter more than one sentence.')
if 'wet' or 'water' or 'liquid' or 'mobile' in answer:
    print('The program has detected that you have entered a query regarding: water') # or anything else wet or whatever
    print('Put your mobile phone inside of a fridge, it sounds stupid but it should work!')
Sign up to request clarification or add additional context in comments.

5 Comments

Thats the thing though, I knew how to do and and that's pretty easy, but instead of saying a query regarding 'water' I was wondering if there is any way to use a list or something to pick out strings from the users sentence and cross reference them, and if there is any matches, it uses them from the list and prints them. However, if there is no matches I am going to have it so they can choose whether or not to write a report to a technician with a report number etc, and this will just write some information to a file
Yes, that very well would work and I know that just from looking at it, because I considered just doing that but it seems pretty basic in my opinion
So what is your expected output? The program has detected that you have entered a query regarding: mobile wet ?
Check @Angel Cruijff's answer that I edited. Does that work for you?
@Angel Cruijff: I changed your answer so that I could access which e matched a word in the input_list.
1

You can do that with a tuple, a list and any function:

SEND_REPORT_TUPLE = ('wet', 'water', 'liquid', 'mobile')
#make a list from the input
input_list = answer.split(" ")
#And then the use any function with comprehension list
if any(e in SEND_REPORT_TUPLE for e in input_list):
    print("The program has detected a query...")

2 Comments

One question, what is a tuple?
@Thom9son A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. >> In the example, you can use a tuple or a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.