我有一个python程序,我正在尝试创建,用户可以在其中输入一个字母,该程序过滤掉所有不以这个字母开头的单词。不幸的是,我和我的初学者大脑都不知道如何用代码来写,所以有什么帮助吗
我的代码已经:
#Open list of words file and add to "content" variable
content = open('Word List').read().splitlines()
#Take the first character of every word and make a new variable to add that to.
firstchar = [x[0] for x in content]
#Ask the user which letter they'd like to use
print("Which letter would you like to use?")
u_selected = input("> ")
不像你看到的那么多,但我很自豪。我想我需要使用firstchar[i]
和u_selected
来匹配这两个字母
如上所述,您可以使用[0]访问字符串的第一个字符。如果符合指定的条件,下面的将为您将每个单词添加到新列表中
.lower()只是将所有内容转换为小写,以确保忽略大小写
要筛选您需要执行的操作,请执行以下操作:
字符串有自己的方法使使用字符串更容易
可以使用.startswith()测试字符串的开头。比如说,
相关问题 更多 >
编程相关推荐