用项目自学python for循环,while循环,if-else条件判断

本文通过Python实现猜数字游戏(for与while循环版本),并演示用户登录模拟及重复错误登录三次后的用户锁定功能。适合初学者练习循环结构与用户交互。

学习目录:

1.猜数字大小游戏 for循环版本

2.猜数字大小游戏while循环版本

3.用户登陆模拟

4.作业:重复错误登陆三次,锁定用户

 

开始项目:

猜数字大小游戏 for循环版本:

for i in range(3):
    age_of_oldboy = 65
    age = int(input("guess age:"))

    if age ==age_of_oldboy:
        print("right! you get it!")
        break
    elif age < age_of_oldboy:
        print("too small")
    else:
        print("too big")
else:
    print("tried too mant times, fuck off...")

 

 

猜数字大小游戏while循环版本:

account = 0
while account < 3:
    account +=1
    age_of_oldboy = 65
    age = int(input("guess age:"))

    if age ==age_of_oldboy:
        print("right! you get it!")
        break
    elif age < age_of_oldboy:
        print("too small")
    else:
        print("too big")
else:
    print("tried too mant times, fuck off...")

用户登陆模拟:

account = 0
while account < 3:
    account +=1
    age_of_oldboy = 65
    age = int(input("guess age:"))

    if age ==age_of_oldboy:
        print("right! you get it!")
        break
    elif age < age_of_oldboy:
        print("too small")
    else:
        print("too big")

    if account ==3:
        continue_comfire = input("do you want to keep guessing..y/n?")
        if continue_comfire != "n":
            account = 0

 

重复错误登陆三次,锁定用户:

count = 0
while 1:
    _user_name = "han"
    _password = "123456"

    user_name = input('user_name:')
    password = input('password:')

    if _user_name ==user_name:
        print("welcome {name} to login..".format(name = user_name))
        break
    else:
        print("invalid username or password!")
        count +=1

    if count == 3:
            print('user locking !')
            if _user_name ==user_name:
                print('user locking !')

 

---恢复内容结束---

转载于:https://www.cnblogs.com/xiaomangxie/p/8533190.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值