私は既に.txtファイルから読み込まれているリストを持っているプログラムを作ろうとしています。今ではPythonに "その文字で始まるすべてのアイテムを別のリストに入れます。事前にユーザーが指定した文字で始まるリスト内の項目を検索
おかげ
countrypopList = open("E:\\SCRIPTING\\Countries.txt").readlines() #This is making "countrypopList" contain everthing on the txt.
countrypopList.sort() #This is sorting the list alphabetically.
useriList = []
countries = []
population = []
userI = input("Please input a single letter: ") #Ask user to input a single letter.
if userI.isalpha():
if len(userI) > 1:
print("Please enter only a single letter.") #If user input length is over 1, display this message.
else: #If there are no complications and the user inputs a single letter as required, the program continues.
#print("continue")
for line in countrypopList:
if (line.startswith(userI)):
useriList.append(line)
print(useriList)
else:
print("Please make sure that you enter a single letter.") #If user inputs anything other than a letter with a length of 1, display this message.
これは私が最初に考え何ですが、それは仕事をdidntの、私はどこで行くことを知らない...
「機能しませんでした」は問題の説明が不十分です。あなたが持っている問題を説明してください。 See [ask]。 – khelwood
'str'の' .startswith'メソッドを使用します – BloodyD