Python 如何获取for循环以检查输入是否包含字母
这是我到目前为止的代码,但它给了我错误Python 如何获取for循环以检查输入是否包含字母,python,for-loop,Python,For Loop,这是我到目前为止的代码,但它给了我错误 for letter in message: if letter.isalpha(letter)==True: positions = [(alphabet.find(m), alphabet.find(n)) for m, n in message] 回溯(最近一次呼叫最后一次): 文件“C:\Users\new\Desktop\python\Part 2.py”,第28行,在 如果字母.isalpha(字母)=真: Attri
for letter in message:
if letter.isalpha(letter)==True:
positions = [(alphabet.find(m), alphabet.find(n)) for m, n in message]
回溯(最近一次呼叫最后一次):
文件“C:\Users\new\Desktop\python\Part 2.py”,第28行,在
如果字母.isalpha(字母)=真:
AttributeError:“tuple”对象没有属性“isalpha”
我基本上是想得到,如果第一部分是真的,它只做代码的位置部分。可能是你想要
字母表位置
来自消息元组
。因此,您可以使用列表理解。比如:
Traceback (most recent call last):
File "C:\Users\new\Desktop\python\Part 2.py", line 28, in <module>
if letter.isalpha(letter)==True:
AttributeError: 'tuple' object has no attribute 'isalpha'
您可以尝试以下方法:
positions = [(alphabet.find(m), alphabet.find(n)) for m, n in message if m.isalpha() and n.isalpha()]
for letter in message:
if letter[0].isalpha() and letter[1].isalpha():
positions = [(alphabet.find(m), alphabet.find(n)) for m, n in message]
您也可以尝试以下方法:
positions = [(alphabet.find(m), alphabet.find(n)) for m, n in message if m.isalpha() and n.isalpha()]
for letter in message:
if letter[0].isalpha() and letter[1].isalpha():
positions = [(alphabet.find(m), alphabet.find(n)) for m, n in message]
您正在元组上测试
isalpha()
,请尝试以下操作:如果字母[0]。isalpha()和字母[1]。isalpha():
什么是按摩?你能给出输入吗?你能给出样本输入和输出吗?