多个IF和ELIF条件[Python]

时间:2015-02-21 02:49:58

标签: python if-statement

name="admin"
passw="aaa"

itemone="01"
itemtwo="02"

a=input("Enter your username:")
b=input("Enter your password:")

if(a==name)and(b==passw):
    print("Welcome.")
    c=int(input("Enter Item Code:"))

   if(c==itemone):
    print("Name: ID")
   elif(c==itemtwo):
    print("Name: Mirror")
   else:
    print("Item not found. Try again.")

else:
    print("Username/Password is incorrect.")
    exit()

当" 01"或" 02"如果输入,程序将忽略所有其他代码并指向未找到的项目。再试一次。"

我终于开始工作了!谢谢!!!

3 个答案:

答案 0 :(得分:3)

您正在获取输入并将其转换为整数,然后检查它是否等于字符串。这将返回false。例如:

01=="01"
=> False

"01"=="01"
=> True

您不需要将输入转换为整数。

答案 1 :(得分:2)

正如robert_x44所说,你正在用字符串比较一个整数。

尝试:

itemone=01
itemtwo=02

另外,在你的帖子中,if块没有缩进。它可能只是一个格式错误,但python if语句必须缩进。

答案 2 :(得分:2)

itemoneitemtwo更改为int,或者不要将输入转换为int。现在,您将intstr进行比较,这将无效。

选择以下两项更改之一 - 不要同时进行,或者您只是改变现在的情况(将strint进行比较而不是{{1} }到int s。)

如何仅使用str s

变化:

int

为:

itemone="01"
itemtwo="02"

如何仅使用itemone=1 itemtwo=2 s

变化:

str

为:

c=int(input("Enter Item Code:"))
相关问题