如何修改技术支持系统代码,通过split()识别含指定关键词的完整句子?
Fixing the Technical Support System to Handle Full Sentences
Got it, let's adjust your code so it can recognize keywords within full user sentences instead of only matching single-word inputs. We'll use the split() function to break down the user's input into individual words, then check if our target keywords are present in that list.
Key Modifications:
- Split the user's input into a list of words using
split()right after retrieving the query - Replace exact string matches (
query == 'keyword') with checks for whether the keyword exists in the split word list ('keyword' in words) - Tweak the conditional order to ensure we handle the "quit" case correctly after checking all keywords
Modified Code:
def welcome(): print('Welcome to the automated technical support system.') print('Please describe your problem.') def get_input(): return input().lower() def main(): welcome() while True: query = get_input() # Split input into individual words to check for keywords words = query.split() if 'crashed' in words: print("Are the drivers up to date?") elif 'blue' in words: print("Ah, the blue screen of death. And then what happened?") elif 'hacked' in words: print("You should consider installing anti-virus software.") elif 'bluetooth' in words: print("Have you tried mouthwash?") elif 'windows' in words: print("Ah, i think i see your problem. What version?") elif 'apple' in words: print("You do mean the computer kind?") elif 'spam' in words: print("You Should see if your mail client can filter") elif 'connection' in words: print("Contact Telkom.") elif query == 'quit': break else: # Catch-all for unrecognized keywords (and non-quit inputs) print('Curious, tell me more.') main()
How It Works:
When a user enters something like my pc crashed, split() turns that into the list ['my', 'pc', 'crashed']. The code checks if 'crashed' is in that list (which it is) and triggers the correct response. For inputs without any target keywords (like yes), it falls through to the final else block and shows the default message.
内容的提问来源于stack exchange,提问作者Yusuf Hendricks
相关产品推荐
相关产品推荐

