How to match words in a list with user input in python?

2024/10/8 8:30:33

I am working on program which takes user input and replaces the words in a list with 'x'. eg is the word is sucks and user input is "this word is sucks". the output should be "this word is xxxxx.

this is what i have so far. how can i access the elements in the list and match with the user input?

def main():message = []words = ['drat','crap','sucks']counter = 0userInput = str(input("Enter The Sentense: "))truncatedInput = userInput[:140]sentence =  truncatedInput.split()for i in range(len(sentence)):
Answer
def main():final_message = []words = ['drat','crap','sucks']counter = 0userInput = input("Enter The Sentense: ")  # use raw_input if you're using python2.XtruncatedInput = userInput[:140]sentence =  truncatedInput.split()for word in sentence:if word in words:word = 'x' * len(word)final_message.append(word)print ' '.join(final_message)
https://en.xdnf.cn/q/118716.html

Related Q&A

How to plot a ROC curve using dataframe converted from CSV file

I was trying to plot a ROC curve by using the documentation provided by sklearn. My data is in a CSV file, and it looks like this.It has two classes Goodand Badscreenshot of my CSV fileAnd my code look…

SyntaxError: Non-ASCII character. Python

Could somebody tell me which character is a non-ASCII character in the following:Columns(str) – comma-seperated list of values. Works only if format is tab or xls. For UnitprotKB, some possible column…

A pseudocode algorithm for integer addition based on binary operation

I have tried for ages to come up with a solution but just cant get my head around it.It needs to be based on two integers on the use of standard logical operations which have direct hardware implementa…

How to efficiently split overlapping ranges?

I am looking for an efficient method to split overlapping ranges, I have read many similar questions but none of them solves my problem. The problem is simple, given a list of triplets, the first two e…

pass 2D array to linear regression (sklearn)

I want to pass 2D array to linear regression: x = [[1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 3, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1],[0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0,…

How do I fix this OverflowError?

I keep getting a "OverflowError: math range error". No matter what I input, the result is the same. Im running Python 3.3, and its finding the problem at the last line. How do I fix this? (A…

Pyinstaller subprocess.check_output error

Ive bundled my app with pyinstaller to 2 *.exegui_app.exe (onefile) config.ini \libs (onedir)winservice.exe+ all DLLs and libsWhen I manually install service with command winservice.exe install everyth…

Exception handler to check if inline script for variable worked

I need to add exception handling that considers if line 7 fails because there is no intersection between the query and array brands. Im new to using exception handlers and would appreciate any advice o…

Parameter list with single argument

When testing Python parameter list with a single argument, I found some weird behavior with print.>>> def hi(*x): ... print(x) ... >>> hi() () >>> hi(1,2) (1, 2) >>…

Scatter plot of values in pandas dataframe

I have a pandas dataframe in the following format. I am trying to plot this data based on ClusterAssigned, with probably different colors for 0 and 1. Distance ClusterAssigned23 135 120 …