0

我是 Python 新手,我想制作一个程序,要求用户输入 3 个字母和一个字符串。然后它将打印出以三个字母开头的字符串的所有字母...例如

Three letters: ABC
Text: A beautiful caterpillar crossed the great bridge owned by anthony bishop canister
A beautiful caterpillar
anthony bishop canister

我的代码目前...

ipt1 = raw_input("Three letters: ") ## Asks for three letters
ipt2 = raw_input("Text: ") ## Asks for text
ipt1_split = ipt1.split() ## Converts three letters to list
ipt2_split = ipt2.split() ## Converts text to list

我不知道在此之后该怎么做,我不确定这是否可以使用 python,如果有人可以为我完成代码,那就太好了,我正在考虑制作一个 foo 循环扫描文本并不确定之后要做什么。任何帮助将不胜感激。顺便说一句,这是使用 Python。

我正在写一本书,出于特定原因,这将在此过程中有所帮助。谢谢!

4

1 回答 1

0

您必须遍历所有单词,然后遍历所有字母,然后检查单词是否以字母开头,如下所示:

ipt1 = raw_input("Three letters: ") ## Asks for three letters
ipt2 = raw_input("Text: ") ## Asks for text
ipt1_split = ipt1.split() ## Converts three letters to list
ipt2_split = ipt2.split() ## Converts text to list

for word in ipt2_split:
    for letter in ipt1_split:
        if word.startswith(letter):
            print word
于 2012-09-06T09:42:49.943 回答