如何搜索单词中的特定字母,然后将该单词添加到列表中?

时间:2022-06-30 15:29:42

I have been given a task for some homework and need some help.

我被赋予了一些功课,需要一些帮助。

The task is as follows: A user is asked to enter a sentence, one word at a time. When the letter a is entered(in a word) input is stopped. Whenever the letter a is in a word, add that word to a list, when sentence is finshed, print the list

任务如下:要求用户输入一个句子,一次一个词。输入字母a(单词)输入停止。每当字母a在单词中时,将该单词添加到列表中,当句子被填充时,打印列表

I have thought about it and have ruled out while loops( correct me if I'm wrong) and thought about using for loops/statements.

我已经考虑过这个并排除了循环(如果我错了就纠正我)并考虑使用for循环/语句。

EDIT: here is the pseudocode:

编辑:这是伪代码:

-Create an Empty list

- 创建一个空列表

-initiate response=space ie."while response is not"

- 启动响应=空间即“响应不是”

-append to list

- 列表

-get word from user

- 来自用户的单词

-print list

If anyone could help me it would be greatly appreciated

如果有人能帮助我,我将不胜感激

1 个解决方案

#1


0  

I am not sure if I understand you correctly, but if the word the user inputs contains the letter a, you want that word to be added to a list, this is how you can do that.

我不确定我是否理解正确,但如果用户输入的单词包含字母a,您希望将该单词添加到列表中,这就是您可以这样做的方法。

word = raw_input("Enter word: ")
lst = []
if "a" in word:
    lst.append(word)
    print lst

raw_input id in Python 2, you can use input in Python 3

Python 2中的raw_input id,您可以在Python 3中使用输入

#1


0  

I am not sure if I understand you correctly, but if the word the user inputs contains the letter a, you want that word to be added to a list, this is how you can do that.

我不确定我是否理解正确,但如果用户输入的单词包含字母a,您希望将该单词添加到列表中,这就是您可以这样做的方法。

word = raw_input("Enter word: ")
lst = []
if "a" in word:
    lst.append(word)
    print lst

raw_input id in Python 2, you can use input in Python 3

Python 2中的raw_input id,您可以在Python 3中使用输入