猜年龄 , 可以让用户最多猜三次!
#!/bin/python3
# -*- coding:utf-8 -*-
age = 56
times = 3
guess = 0
while guess != 3:
user_guess = int(input("input your guess age:"))
if user_guess > age :
print("try smaller!")
guess += 1
elif user_guess < age :
print("try bigger!")
guess += 1
else:
print("you got it!")
break
if guess == 3:
print("only can guess three times!")
猜年龄 ,每隔3次,问他一下,还想不想继续玩,y,n
#!/bin/python3
# -*- coding:utf-8 -*-
age = 56
while True:
guess = 0
while guess != 3:
user_guess = int(input("input your guess age:"))
if user_guess > age :
print("try smaller!")
guess += 1
elif user_guess < age :
print("try bigger!")
guess += 1
else:
print("you got it!")
break
if guess == 3:
print("only can guess three times!")
flag = input("continue play?y/n:")
if flag == 'n' or flag == 'N':
break
登陆接口
输入用户名密码,认证成功后显示欢迎信息,输错三次后锁定
#!/bin/python3
# -*- coding:utf-8 -*-
#调用
import getpass,os
def read_users(username):
flag = 1
user_list = open('userlockd.txt', 'r')
while True:
line=user_list.readline()
if len(line.strip()) == 0:
break
if line.strip() == username:
flag = 0
break
return flag
def lock_user(username):
user_list = open('userlockd.txt','w+')
user_list.writelines(username)
times = 0
while True:
username = input("username:")
password = getpass.getpass("password:")
if read_users(username):
if username == "sbegon" and password == "123":
print("welcome %s" %username)
break
elif times == 2:
print("wrong username or password!this account has been locked!")
lock_user(username)
break
else:
print("wrong username or password!")
times += 1
else:
print("this account has been locked")
break