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

时间:2022-12-13 12:19:30

学习目录:

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...")

用户登陆模拟:

_usename = 'han'
_password = '123456'

usename = input('usename:')
password = input ('password:')

if _usename == usename and password == _password:
print("welcome user {name} login..".format(name=usename))
else:
print ("Invalid username or password")

 

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

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 !')