首页 > 解决方案 > 我的程序要求用户输入密码,如果用户输入错误的密码,他会尝试五次,然后程序告诉它结束

问题描述

我得到错误的输出。即使用户重新输入了正确的密码,循环也不会中断。当用户输入正确的密码时,它不显示登录成功。

count = 0
password = input('Enter password: ')
while password != 'abcdefghijkl' and count <= 5:
    password_renter = input('Enter the password: ')
    count = count + 1
    if password_renter == 'abcdefghijkl' and password == 'abcdefghijkl':
        print('login successful')
        break
    if password_renter != 'abcdefghijkl' and count > 5:
        print('Chances over')

标签: pythonpython-3.x

解决方案


您在 while 循环内输入的密码不等于“abcdefghijkl”。因此,当您要在 while 循环中检查它以查看 password_renter 和 password 是否相等时,它始终为 False,因为右侧为 False。

我建议最小的变化。但是,我不确定这是否是您想要的。请在您的最后进行测试。

count = 0
password = input('Enter password: ')
while count <= 5:
    password_renter = input('Enter the password: ')
    count = count + 1
    if password_renter == 'abcdefghijkl' and password == 'abcdefghijkl':
        print('login successful')
        break
    if password_renter != 'abcdefghijkl' and count > 5:
        print('Chances over')

推荐阅读