2016-09-17 40 views
-2

由于某种原因,如果没有else语句,代码将查找字符串中字符的索引。但是,当我添加else语句来声明是否找不到字符时。它所做的就是给我else语句,即使字符在字符串中。If else用于查找字符串中字符索引的语句

#Function takes a character and a string and returns the index of the character 
#found in the string. 
def whereIsItS(letter, word): 
    #Start finding the index of characters in the string starting at 0. 
    letInWord = 0 
    #For loop to find specific characters in a string. 
    for l in word: 
     #If the letter is found it returns the index of the letter. 
     if l == letter: 
     #Moves to the next character in the string. 
      letInWord += 1 
     else: 
      return "It is not there." 
    #Returns the index of the character found the string.   
    return word.index(letter) 

我似乎无法弄清楚为什么它没有else语句,但没有else语句。

+0

因为如果该单词的第一个字母不是所需的字母;它立即返回。你可以做更好的计算索引。我想你想避免'index'返回的异常。 –

+1

你的函数有11行代码(我计算!),这些代码是毫无意义的,然后它返回内建'index'方法的结果。为什么? – zvone

+0

我仍然处于编程初期,并且认为我必须通过字符串中的字符来计算for循环以获取索引。我仅在2周前开始>< – Wiggs

回答

1

return将从函数总是退出,而不是继续要执行任何循环。

你要么print它不存在,但这不会是真的,因为你正在打印当前的信件。

这是比较简单的实现与以下内容:

def whereIsItS(letter, word): 
    if letter in word: 
     return word.index(letter) 
    else: 
     return "Letter {0} not in word {1}".format(letter, word) 

如果字母是词,返回它的索引,如果没有,返回消息指定它没有被发现。

与条件表达式进一步精简为:

def whereIsItS(letter, word): 
    return word.index(letter) if letter in word else "Letter {0} not in word {1}".format(letter, word) 
+0

你如果别人的声明为我工作。非常感谢。 :) – Wiggs

0

您正在返回else的条件。 更改return "It is not there."print("It is not there.")

+0

我遇到了打印问题,因为它会多次重复打印的字符串,直到达到目标字符串的末尾。 – Wiggs

1

这是有问题的代码:

for l in word: 
    #If the letter is found it returns the index of the letter. 
    if l == letter: 
    #Moves to the next character in the string. 
     letInWord += 1 
    else: 
     # You end the loop here 
     # return "It is not there." 
     # try this: 
     print("It is not there.") 

正如你所看到的,return退出在环中间的功能。如果你不想离开for循环,你应该使用print()

请记住,你可以只使用word.index(letter),并不需要一个完整的循环,像这样:

def whereIsItS(letter, word): 
    try: 
     return word.index(letter) 
    except ValueError: 
     print("letter not in string") 
+0

这与吉姆的解释一起帮助了我。非常感谢。 :) 我有多次在打印机中重复打印问题(可能是因为for循环)。 – Wiggs

0

你的错误已被我等&人在其他的答案解释。

现在让我们避免做不太有效的代码。这里是做 (仍是

  1. 使用enumerate获得指数和价值的3种有效的方式第一次出现:你可以返回指数

代码:

def whereIsItS(letter, word): 
    #Start finding the index of characters in the string starting at 0. 
    #For loop to find specific characters in a string. 
    for i,l in enumerate(word): 
     #If the letter is found it returns the index of the letter. 
     if l == letter: 
      return i 
    return "It is not there." 
  1. 尝试index并发现异常(似乎您的预检是为了避免此异常)

代码:

def whereIsItS(letter, word): 
    try: 
     return word.index(letter) 
    except ValueError: 
     return "It is not there." 
  • 使用str.find。它会查找子,因此它可以寻找一个单一信太(而不像index不抛出异常,但只是返回-1
  • 代码:

    def whereIsItS(letter, word): 
        idx = word.find(letter) 
        if idx>=0: 
         return idx 
        else: 
         return "It is not there." 
    
    当然

    ,返回值时,信不在这里真的不实际。我会选择-1(如find)或None

    +0

    你们是最棒的。感谢您的帮助和快速回复。 :) – Wiggs

    相关问题