【问题标题】:conditional statement acting abnormal条件语句动作异常
【发布时间】:2020-01-21 01:54:08
【问题描述】:
def signUp():
    id=input("enter your id")
    password=input("enter your password")
    if (len(id) == 9 & len(password) > 4):
        print("YOUR ACCOUNT IS CREATED.....")
    else:
        print("Invalid ID or password")

虽然我提供了长度为 9 的 ID 和长度大于 4 的密码,但条件仍然不起作用。它总是会发生。但是如果我对 ID 和密码使用嵌套的 if,它就可以正常工作。

你能帮忙吗?

【问题讨论】:

  • 在此处使用 and 关键字,而不是 & 运算符。 & 是一个按位“与”运算,适用于整数,并且优先级高于 ==,所以这段代码绝对不是你想要的。
  • 这能回答你的问题吗? Are & and <and> equivalent in python?

标签: python-3.x conditional-statements


【解决方案1】:

&amp; 是按位与。你想要逻辑and

if len(id) == 9 and len(password) > 4:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-16
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多