2017-02-04 34 views
0

我试图解决python挑战的第三级(http://www.pythonchallenge.com/index.php如何分别处理两个相同的字符?

我写了一些代码来解决挑战。

text = "EXAcTLYdsgsdcTLY" 
    jlist = [] 
    boo = False 

    for i in text: 
     if ord(i) in range(97, 123) and text.index(i) in range(3, len(text) - 2): 
      j0 = text.index(i) 
      j1 = text[j0 + 1] 
      j2 = text[j0 + 2] 
      j3 = text[j0 + 3] 
      j_1 = text[j0 - 1] 
      j_2 = text[j0 - 2] 
      j_3 = text[j0 - 3] 

      jlist = [j_3, j_2, j_1, j1, j2, j3] 

      boo = False 

     for j in jlist: 
      if ord(j) in range(65, 91): 
       boo = True 
      else: 
       boo = False 
       break 

     if boo == True: 
      print(i) 

现在问题是在代码中有两个'c'。为第一个c生成的jlist是为第二个c生成的,而不是为它创建一个新的jlist。

The code output

+1

1.什么问题你是如此在这里? 2.你的输出是**而不是图像**。请将纯文本输出复制到您的文章中。 – usr2564301

+2

'如果boo == True'?为什么不'if(boo == True)== True'? – melpomene

+0

@melpomene他们是完全一样的,都返回相同的值 – Zeus3101

回答

0

这是因为字符第一次出现index回报,所以这个任务将给予第二c错误的值:

 j0 = text.index(i) 

相反,拿在for位置声明,使用enumerate()

for j0, i in enumerate(text): 
    if ord(i) in range(97, 123) and text.index(i) in range(3, len(text) - 2): 
     # no need to calculate j0 now. 
+0

感谢您的帮助。我想知道如果index()实际上是第一次发生。这固定了它。 – Zeus3101

+0

不要忘记[将答案标记为已接受](http://stackoverflow.com/help/someone-answers)。 – trincot

相关问题