python 循环使用 while 或 for 语句实现用户名密码输错三次退出


如有错误欢迎大家指出,新手初来乍到。程序没那么复杂,是最简单的。

一、需求

编写登录文件 .py
1. 输入用户名密码
2. 正确,输出欢迎登录
3. 当输入用户名和密码小于 3 次,输入用户名或者密码错误,提示用户名或者密码错误。再次输入用户名和密码,剩余输入次
数。
3. 当输错三次后退出

二、流程图

 

三、代码

for

 

#!/usr/bin/env python
#_*_conding:utf-8_*_



user = "zhangjinglei"
password = "lei100103"
count = 0;
for i in range(3):    
    username = input("username:")
    password = input("password:")
    if username == user and password == password:
        print("Welcome Login")
        count = 3
        break
    else:
        print("Wrong username or password")
        count += 1
        print("you can try", 2 - i, "times")

 

while

#!/usr/bin/env python
#_*_conding:utf-8_*_



user = "zhangjinglei"
password = "lei100103"
count = 0;
while count < 3:    
    username = input("username:")
    password = input("password:")
    if username == user and password == password:
        print("Welcome Login")
        count = 3
    else:
        print("Wrong username or password")
        count += 1
        print("you can try", 3 - count, "times")

四、验证结果
1.for 验证结果

2.whlie验证结果

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM