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')
运行时显示:
TypeError: Can't convert 'int' object to str implicitly
它应打印出'a'
如何修复该功能,或如何检查列表中是否有两个连续的字符?
答案 0 :(得分:0)
使用ord
和chr
函数获取后续字符:
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')
答案 1 :(得分: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 如在迭代器中