2014-10-01 22 views
-1
prblm = input("Input the Blackout math problem: ") 
#I'm using 2//4=5 

OP = ['+', '-', '*', '/'] 

def validate(): 

    """Checks to see if a string is valid""" 
     for c in prblm: 
      if c in OP and c+1 in OP: 
       print('a') 

运行时里显示:检查两个consectutive字符是一个列表

TypeError: Can't convert 'int' object to str implicitly 

应该打印出“一个”

如何修复功能的工作或我该怎么办检查两个连续的字符是否在列表中?

+0

如果你找到一个解决问题的方法,只是将它张贴作为一个答案,不将其粘贴在你原来的问题 – 2015-04-07 21:53:08

回答

0

使用ordchr函数来获得后续字符:

def validate(): 
    """Checks to see if a string is valid""" 
     for c in prblm: 
      if c in OP and chr(ord(c)+1) in OP: 
       print('a') 
+0

我试了一下,有没有错误,但无法打印 – 2014-10-01 03:01:24

+0

您输入的数据是否包含两个独立的字符? – crhodes 2014-10-01 11:52:55

0

正如编辑对他的质询时所用@AlexCampbell:

prblm = input("Input the Blackout math problem: ") 
#I'm using 2//4=5 

OP = ['+', '-', '*', '/'] 

def validate(): 

    """Checks to see if a string is valid""" 
     for c in range (0, len(prblm)-2): 
      if c in OP and c+1 in OP: 
       print('a') 

固定它。必须使用范围功能。前:C是一个EL现在:用c 在迭代

相关问题