【发布时间】:2021-07-15 19:09:01
【问题描述】:
我正在尝试创建一个函数hasFinalLetter,它接受两个输入并创建strList 中所有以字母结尾的字符串的列表。
然后创建三个带有字符串的实际列表,以证明函数hasFinalLetter 有效,其中一个实际列表必须给出空输出。
def hasFinalLetter(strList, letters):
strList2 = []
for strings in strList:
length = len(strings)
lastLetterString = strings[length - 1]
for lastLetter in letters:
if lastLetterString == lastLetter:
strList2.append(strings)
return strList2
#This will return empty list
people = ["Isaac", "Mitchel", "Priscilla", "Michael"]
lettersForPeople = "bLA"
print(hasFinalLetter(people, lettersForPeople))
# Case 2
things = ["Cars", "Phone", "box", "laptop"]
lettersForThings = "sexe"
print(hasFinalLetter(things, lettersForThings))
# Case 3
things2 = ["Car", "Phone", "box", "Laptop"]
lettersForThings2 = "rexp"
print(hasFinalLetter(things2, lettersForThings2))
但是,我的定义中有一些东西使我的代码输出只是:
[]
[]
['Laptop']
虽然其他都是空白的,即使lettersForThings2 包含things2 中的所有最后一个单词。
我已经调试了很长时间,似乎无法找到问题的根源。
【问题讨论】:
-
是的,我试过了,仍然给出相同的输出
-
拜托了!使用
has_final_letter而不是hasFinalLetter,它根本不是pythonic -
@DevLounge 在我的学校我们使用
hasFinalLetter