11.6k views
4 votes
In Python please:

Write a program that reads a character, then reads in a list of words. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character.
Ex: If the input is:
z
hello zoo sleep drizzle
the output is:
zoo
drizzle
Keep in mind that the character 'a' is not equal to the character 'A'. Do not end with newline.

User Brittanie
by
7.6k points

1 Answer

4 votes

Answer:

Here's an example Python program that achieves the desired functionality:

# Read in the character to search for

char = input().strip()

# Read in the list of words

words = input().strip().split()

# Loop through each word in the list and check if it contains the character

for word in words:

if char in word:

print(word, end=' ')

Step-by-step explanation:

The program first reads in the character to search for using input(). This is stored in the variable char.

Next, the program reads in the list of words using input(), strips off any leading/trailing whitespace with .strip(), and splits the string into a list of words using .split(). This list of words is stored in the variable words.

The program then loops through each word in the words list using a for loop. For each word, the program checks if the character specified by the user is present in the word using the in operator. If the character is present, the program prints the word followed by a space (using the end parameter of the print() function).

Note that the end parameter of the print() function is set to a space instead of the default newline (\\) character, as specified in the problem prompt.

User Justin Meltzer
by
9.0k points