'searching multiple words from dict and creating null entry check
I need to find a way to return a sentence based upon the input of the user i.e key word search.
I have a dictionary created and can return a sentence based on one word but cant figure out if I can return a sentence based on multiple words:
water damage returns a sentence on dropping your phone into water i have a cracked screen does not return anything. I am aware that the issue is around the .split.strip functions I am using.
My next issue is that I can not seem to create a null entry check, I have tried the usual, while input_1 is None, or =='' yet the strip function removes whitespace so I am guessing there is no null entry to pick up on.
similar_words = {
'water': 'you have let water into your phone',
'wet': 'let your phone dry out then try to restrat the phone',
'crack case': 'you have cracked your screen or case, this will need replacing by a specialist',
'cracked screen': 'you have cracked your screen or case, this will need replacing by a specialist',
'turn on': 'your battery may need replacing',
'crack': 'your phone screen has been cracked, you need to contact the technician centre',
}
def check():
if word.lower() in similar_words:
print(similar_words[word.lower()])
input_1 = input("What seems to be the problem with your phone?: ").strip().split()
for word in input_1:
check()
def close():
print ('Please press enter to close the program')
quit()
close_1 = input('Have we addressed your problem, please answer yes or no?: ')
if close_1=='yes':
close()
else:
print ('Lets us move on then')
Solution 1:[1]
According to your program I think that you need to display the phone problem's solution only but your program is not displaying it. So I have done some changes in the program and it is displaying it.
similar_words = {
'water': 'you have let water into your phone',
'wet': 'let your phone dry out then try to restart the phone',
'crack case' : 'you have cracked your screen or case, this will need replacing by a specialist',
'cracked screen' : 'you have cracked your screen or case, this will need replacing by a specialist',
'turn on' : 'your battery may need replacing',
'crack' : 'your phone screen has been cracked, you need to contact the technician centre'
}
input_1 = input("What seems to be the problem with your phone?: ").split()
for word in input_1:
if word in similar_words.keys():
print(similar_words[word])
#similar_words[word] gives only the value of the key word
close_1 = input('Have we addressed your problem, please answer yes or no?: ')
if close_1=='yes':
quit()
else:
print ('Lets us move on then')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharman |
