長さを問わず、リストと照合するチェッカーを使用する関数を作成しようとしています。単語を確認して印刷するときは、大文字と小文字を区別しないでください。リストに対して変数をチェックし、そのリスト内の単語が変数で始まるかどうかを調べる
Input= startsWith('a',['apple','ApPle','orange','Apple','kiwi','apricot'])
出力以下の例は:
apple
ApPle
Apple
apricot
しかし、それはすべて小文字で、リスト内のすべての文字列を出力します。
def startsWith(checker,lister):
checker.lower()
size = len(lister)
i=0
checklength = len(checker)
lister = [element.lower() for element in lister]
while(i<size):
checkinlist = lister[i]
if(checkinlist[0:checklength-1] in checker):
# this is just to test to see if the variables are what i need
# once the if statement works just append them to a new list
# and print that
print(lister[i])
i=i+1
私が手 'NameError:私はあなたのスクリプトを実行するときに名前 'チェッカー' は、defined'されていません。 –
@ tommy.carstensen thatsあなたは関数定義を含まなかったので、現時点で質問に正しく書式設定されていません。 – GreenAsJade
...これで修正されました。 – GreenAsJade