python - 我的程序要求用户输入密码,如果用户输入错误的密码,他会尝试五次,然后程序告诉它结束
问题描述
我得到错误的输出。即使用户重新输入了正确的密码,循环也不会中断。当用户输入正确的密码时,它不显示登录成功。
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')
解决方案
您在 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')
推荐阅读
- mongodb - 如何摆脱 MongoDB Compass Aggregation Builder 中的连接超时?
- node.js - 如何使用 Firebase 在 Express 中修复“错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法删除标头”?
- python - 获得排序数组所需的最小交换
- php - 如何使用 htaccess 重写规则简化 PHP 站点 URL?
- json - 无法使用正则表达式匹配整数值 - 错误(在
:6): 数字不是字符串或数组 - python - 如何操作数据框中的移位函数以跳过行并仅移位选择的行
- spring-boot - 如何在 Android Studio 中正确停止 Spring Boot 应用程序?
- django - 如何在弹性搜索中运行查询和过滤的组合?
- cmd - 使用 cmd findstr 时,如何在文本文件中实际找不到匹配项时输出“未找到匹配项”?
- python - 在中间步骤中引用链式 Pandas DF