python-3.x 为什么else函数在elif语句为true时仍运行?[closed]

z31licg0  于 2022-11-19  发布在  Python
关注(0)|答案(1)|浏览(152)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受答案。

这个问题是由一个打字错误或一个无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
4天前关闭。
Improve this question
我正在做一个带回家的考试,我完成了它,但只有一个样本测试它不工作。下面是我的代码:

h = 'abcdefgh'
v = '12345678'
h_knight = (input('Please enter horizontal position of the knight (a,b,c,d,e,f,g,h): ')).lower()
if len(h_knight) != 1 or h_knight.isalpha() is False:
  print('Horizontal input for knight is not a letter')  
elif not (h_knight) in h:
  print('Horizontal input for knight is not a proper letter')
else:  
  v_knight = (input('Please enter vertical position of the knight (1,2,3,4,5,6,7,8): '))
  if v_knight.isdigit() == False:
    print('Vertical input for knight is not a number')
  elif v_knight.isdigit() == True and v_knight not in v:
    print('Vertical input for knight is not a proper number')
  else:
    h_bishop = (input('Please enter horizontal position of the bishop (a,b,c,d,e,f,g,h): ')).lower()
    if len(h_bishop) != 1 or h_bishop.isalpha() is False:
      print('Horizontal input for bishop is not a letter')
    elif not (h_bishop) in h:
      print('Horizontal input for bishop is not a proper letter')
    else:    
      v_bishop = (input('Please enter vertical position of the bishop (1,2,3,4,5,6,7,8): '))
      if v_bishop.isdigit() == False:
        print('Vertical input for bishop is not a number')
      elif v_bishop.isdigit() == True and v_knight not in v:
        print('Vertical input for bishop is not a proper number')
      else:
        idx1 = h.find(h_knight)
        idx2 = v.find(v_knight)
        idx3 = h.find(h_bishop)
        idx4 = v.find(v_bishop)
        if idx1 == idx3 and idx2 == idx4:
          print("They can't be in the same square")
        elif abs(idx1 - idx3) == 2 and abs(idx2 - idx4) == 1:
          print('Knight can attack bishop')
        elif abs(idx1 - idx3) == 1 and abs(idx2 - idx4) == 2:
          print('Knight can attack bishop')
        else:
          if abs(idx1 - idx3) != abs(idx2 - idx4):
            print('Neither of them can attack each other')
          else:
            print('Bishop can attack knight')

不起作用的样品测试的输入:h,1,h,9。它应该显示“垂直输入的主教是不是一个适当的数字”,但它却显示“他们都不能攻击对方”,这是一个其他条件,正如你可以看到上面。我感谢任何建议,以解决它。

gopyfrb3

gopyfrb31#

您从未检查象位置是否在v

elif v_bishop.isdigit() == True and v_knight not in v:
                print('Vertical input for bishop is not a proper number')

应该是这样的:

elif v_bishop.isdigit() == True and v_bishop not in v:
                print('Vertical input for bishop is not a proper number')

我猜你是复制粘贴的,没有修改

相关问题