Skip to main content
 首页 » 编程设计

python之检查输入中是否有字母

2025年05月04日11wuhuacong

我的错误出现在这一行:

if exclude3 not in Sent: 

它是:

TypeError: 'in <string>' requires string as left operand, not set 

我的代码是:

import string 
 
Word = input("Please give a word from the sentence") 
 
exclude3 = set(string.ascii_letters) 
 
if exclude3 not in Sent: 
    print("") 
elif exclude3 not in Word: 
    print("") 
else: 

什么是左操作数?我做错了什么,有没有更简单的方法来完成我想要的?我应该使用 in 以外的东西吗?应该是什么?

请您参考如下方法:

exclude3 不是一个 string 它是一个 set。
您尝试使用 in 运算符来检查 set 是否包含在另一个 set 中,这是错误的。

也许你打算写:if Sent not in exclude3 ?