# #作业1
# 猜年龄,可以让用户猜三次!
age = 25
user_guess = int(input("input your guess"))
age = 25
count = 0
while count <3 :
user_guess = int(input("please enter an age:"))
if (user_guess == age):
print ("Corrcet!")
break
elif (user_guess < age):
print ("smaller!")
else:
print ("bigger!")
count +=1
# #作业2
# 猜年龄,每隔三次,问他一下,还想不想玩,y,n
print("You have only 3 chances!")
age_of_oldboy = 56
count = 0
while count <3:
guess_age = int(input("Guess Age:"))
if guess_age == age_of_oldboy :
print("Yes,you got it.")
break
elif guess_age > age_of_oldboy:
print("Bigger!")
elif guess_age < age_of_oldboy:
print("Smaller!")
count +=1
if count == 3:
countine_confirm = input("Do you want to contonue?")
if countine_confirm !='n':
count =0
else:
print("Log out!")
# #作业3
# 用户登录程序 ,编写登录接口
# 输入用户名密码
# 认证成功后显示欢迎信息
# 输错三次后锁定
法一:
import os
count = 1
name =
"tom"
passwd =
"123456"
while
True:
guess_name = input(
"please input your name: "
)
guess_passwd = input(
"please input your password: "
)
if
guess_name == name and guess_passwd == passwd:
print(
"welcome.."
)
exit()
elif count >= 3:
os.system(
"sed -i '/tom/s/bin/bash/sbin/nologin/' /etc/passwd"
)
with open(
'/etc/nologin.txt'
,
'w'
)
as
f:
f.write(
"Your account is locked"
)
f.close()
print(
"Your account is locked...哈哈"
)
exit()
else
:
print(
"try agin.."
)
count = count + 1
法二:
count = 0 #记录用户输入密码的次数
flag = 1 #标志位
lock = []
user_pass = []
username = input('please input your name:')
#读取黑名单的内容
f = open('black_mingdan','r')
lock_file = f.readlines()
f.close()
#将黑名单文件内容作为列表元素追加到列表中
for i in lock_file:
line = i.strip('
')
lock.append(line)
#若输入的用户名在黑名单中,如果在则给出提示信息:用户已经被锁定,请联系管理员。
if username in lock:
print('User %s Have Been Locked.It is not allow to login,please contact Administrator.' %username)
else:
#输入的用户名不在黑名单中,则提示用户输入密码信息
while True:
count += 1
passwd = input("please input your password:")
f = open('user_information','r')
user_file = f.readlines()
f.close()
for i in user_file:
user_pass = i.strip().split()
#判断输入的用户名==user_pass[0] and 密码==user_pass[1],如果相等,则提示欢迎信息并退出循环,如果不相等则
#结束本次循环
if username == user_pass[0] and passwd == user_pass[1]:
print('welcome user %s login !' %username)
flag = True
break
else:
continue
#若flag为真,则用户名和密码输入正确跳出整个循环体,反之,若用户输入密码错误的次数为3,则给出提示信息:用户已经被锁定
#并将username追加到黑名单中
if flag is True:
break
else:
if count == 3:
print('User Have Been try 3 times,Have Been Locked')
lock_file = open('black_mingdan','a')
#lock_file.write('Have Been Locked User:%s
' %username)
lock_file.write('%s
' %username)
lock_file.close()
break