While循环不循环-Python

时间:2019-09-30 10:34:11

标签: python-3.x python-2.7

我的代码中的while循环没有循环。

我尝试确保while循环会循环,但不执行任何循环。

#Name_Input
login = 1
while login == 1:
    print("Enter the username and password \n")
    username = input("Username: ")
    password = input("Password: ")
    if password and username != "cameron" and "123":
        print("\nWrong username or password... \nTry Again...")
        login = 1

我期望在错误输入登录详细信息时循环返回到登录。

2 个答案:

答案 0 :(得分:2)

login = 1
while login == 1:
    print("Enter the username and password \n")
    username = str(input("Username: "))
    password = str(input("Password: "))
    if username  ==  "cameron" and   password == "123":
        login=2
    else:
        print("\nWrong username or password... \nTry Again...")

答案 1 :(得分:1)

请尝试以下语法:

login = 1
while login == 1:
    print("Enter the username and password \n")
    username = input("Username: ")
    password = input("Password: ")
    if password !=  "123" or username != "cameron":
        print("\nWrong username or password... \nTry Again...")
        login = 1
    else:
        login = -1