简体   繁体   English

在python中使用def创建函数的问题

[英]Problem with using the def to create a function in python

I'm trying to create a function hasFinalLetter which takes two inputs and creates a list of all the strings in strList that ends with a letter in letters.我正在尝试创建一个函数hasFinalLetter ,它接受两个输入并创建 strList 中所有字符串的列表,该列表以字母中的字母结尾。 Then create three actual lists with strings to prove that the function hasFinalLetter works and one of the actual lists has to give an empty output.然后创建三个带有字符串的实际列表以证明函数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))

However, there is something in my definition that makes my code output just:但是,我的定义中有一些东西使我的代码输出只是:

[]
[]
['Laptop']

While the others are blank, even when lettersForThings2 contains all the last words in things2 .而其他都是空白,即使lettersForThings2包含在所有的遗言things2 I've been debugging for quite some time now and can't seem to find the source of the problem.我已经调试了很长时间,似乎无法找到问题的根源。

There is bad indentation in function hasFinalLetter :函数hasFinalLetter有错误的缩进:

def hasFinalLetter(strList, letters):
    strList2 = []
    for strings in strList:
        length = len(strings)
        lastLetterString = strings[length - 1]

        # bad indentation was here:
        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))

Prints:印刷:

[]
['Cars', 'Phone', 'Phone', 'box']
['Car', 'Phone', 'box', 'Laptop']

Please use the code below.请使用下面的代码。

   def hasFinalLetter(strList, letters):

      strList2 = [ ]
      lastLetterString = [ ]

      for strings, letter in zip(strList, letters):

          length = len(strings)

          lastLetterStr = strings[length-1]

          lastLetterString.append(lastLetterStr)

          if lastLetterStr == letter:
              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))

Output:输出:

  [ ]
  ['Cars', 'Phone', 'box' ]
  ['Car', 'Phone', 'box' , 'Laptop']

Please redefine your function as shown above:请重新定义您的函数,如上所示:

I tried sending a picture it didn't work since I'm new to Github, also tried commenting it didn't work too.....由于我是 Github 新手,我尝试发送它不起作用的图片,也尝试评论它也不起作用.....

You can send a mail personally to me via kelvinlikel@gmail.com I got the exact output I believe you wish to have您可以通过 kelvinlikel@gmail.com 亲自给我发送邮件我得到了我相信您希望拥有的确切输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM