python - 如何在 Python 中创建一个密码来结束被拒绝的代码的以下部分?
问题描述
我有我的密码和所有密码,但如果您没有正确的密码,我希望有以下部分代码,您无法访问。我怎么做?
attempts=0
while attempts<3:
username=input('Username?')
password=input('Password?')
birhdate=input("Birthdate?")
pin=input("Enter your four digit pin.")
if username=='l.urban'and password=='lucasur2'and birhdate=='857585'and
pin=='1973':
print('you are in!')
else:
attempts+=1
print('incorrect!')
if attempts==3:
print('too many attempts')
end code
else:
attempts+=1
print('incorrect!')
if attempts==3:
print('too many attempts')
end code
Python 3.6.1(默认,2015 年 12 月,13:05:11)Linux 上的 [GCC 4.8.2] 文件“main.py”,第 14 行`在此处输入代码结束代码 ^``IndentationError:意外缩进 </p >
解决方案
确保正确缩进你的代码,否则 Python 将无法解析它,你会得到IndentationError
.
修复缩进后,您的代码应该可以工作,但如果您想学习,这里有一个简化版本:
def check_user(username: str, password: str) -> bool:
return username == 'admin' and password == 'password'
def main():
tries = 1
while True:
if tries > 3:
print('Too many attempts')
return
username = input('username?')
password = input('password?')
if check_user(username, password):
break
else:
print('Invalid credentials')
tries += 1
continue
print("You're in")
# do some work
if __name__ == '__main__':
main()
推荐阅读
- google-cloud-platform - 从后端服务到达负载均衡器时,TCP 负载均衡器连接被拒绝
- python - 如何使用 **Kwargs 进行可选值输入
- nginx - 用于 Magento2 网站的带有 CDN 的 html 内容的 CORS 错误
- java - 无法使用 XMLEventReader 将 XML 解组到我的类
- mongodb - 使用 Spring Data 将存储在 H2 中的实体与存储在 MongoDB 中的文档连接起来
- python - “GET /socket.io/?EIO=3&transport=websocket HTTP/1.1”错误
- aws-elasticsearch - 如何将文件数据转储到 AWS ElasticSearch
- angular - 在我的角度应用程序中将 null 传递给 ReplaySubject.next() 时,我遇到了一个错误,我不明白为什么。帮助表示赞赏
- amazon-web-services - 使用 AWS 将两个源 DynamoDB 表转换为新的 DynamoDB
- java - 交点以什么比例分割线段?