【问题标题】:TypeError: 'in <string>' requires string as left operand, not tuple. ErrorTypeError: 'in <string>' 需要字符串作为左操作数,而不是元组。错误
【发布时间】:2021-06-25 16:22:49
【问题描述】:

我是编程初学者,我正在尝试为“包含”变量分配多个值,但我不断收到:“TypeError:'in' 需要字符串作为左操作数,而不是元组。” 谁能告诉我这意味着什么以及如何解决它? 这是程序(tkinter):

def myClick():
password = e.get()
contains = "1", "2"

if contains in password:
    myLabel = Label(root, text= "Password Level is Strong")
    myLabel.pack()

【问题讨论】:

  • 您可以使用if any(x in password for x in matches):,其中匹配项是['1','2']
  • @TheLizzard 非常感谢,成功了!
  • @lucal 没问题。我以为您正在寻找类似@ Sujay 所说的内容,所以我删除了我的评论:D。

标签: python


【解决方案1】:

Python 给你这个消息是因为你试图将元组与字符串进行比较,但你需要将字符串与元组进行比较.. 例如与你的代码:

if password in contains:
    myLabel = Label(root, text= "Password Level is Strong")
    myLabel.pack()

如果您想使用元组或列表,我建议您使用“for 循环”..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    相关资源
    最近更新 更多