######用户登陆脚本########
#!/bin/usr/env python
#coding = utf-8
from time import time,ctime
import getpass
db={"kang":["amin",0],"yiyi":["bushe",0]}
def menu():
while True:
print '''
(A)dd user
(L)ogin user
(D)elete user
(S)how user
(E)xit
'''
choice=raw_input("please input your choice:").lower()
if choice == "d":
deluser()
elif choice == "a":
adduser()
elif choice == "l":
login()
elif choice == "s":
showuser()
elif choice == "e":
exit(0)
else:
print "input error!"
def adduser():
while True:
name=raw_input("please input username:")
pwd=getpass.getpass("please input password:")
if name in db.keys():
print "the %s user is exist!" %name
continue
else:
break
logintime=time()
db[name]=[pwd,logintime]
def login():
for i in range(3):
name=raw_input("login username:")
pwd=getpass.getpass("passwd:")
if name in db.keys():
if pwd==db.get(name)[0]:
print "login success"
print "the last login time in at:",ctime(db[name][1])
current=time()
delta=current-db[name][1]
if delta<=14400:
print "You already logged in 4 hours period!"
else:
logintime=time()
db[name][1]=logintime
break
else:
print "login incorrect!"
else:
print "login incorrect"
def deluser():
name=raw_input("please input delete username:")
if name in db.keys():
db.pop(name)
print "the username delete success!"
else:
print "the username is not exist!"
def showuser():
for k,v in db.items():
print "name=%s pass=%s" %(k,v)
menu()
######函数#########
def abs(x)
if x>0:
print x
else:
print -x
def abs(x)
if x>0:
return x
else:
return -x
当你print abs(-10)的时候 如果没有返回值的话他会返回一个NOne 如果有返回值的话他会返回返回值
python中函数还可以返回多个返回值 有多个返回值的时候他返回的会是一个元组,然后我们可以对返回的元组进行操作
def f():
a=1
b=2
c=3
return a,b,c
当调用f()的时候
In [4]: f()
Out[4]: (1, 2, 3)
In [5]: a,b,c=f()
In [6]: print a
1
In [7]: print b
2
In [8]: print c
3
In [26]: def power(x,y):
....: return x**y
In [27]: power(2,3)
Out[27]: 8
###可变参数###
#!/usr/bin/env python
#coding:utf-8
def add(*args):
print type(args) ##类型是一个元组
sum=0
for i in args:
sum+=i
return sum
L=[1,2,3,4]
print add(*L)
assert add(1,2)==3 ##断言hello(1,2)的返回值是不是3
[root@foundation14 code]# python args.py
<type 'tuple'>
10
找出最长字符串
#/usr/bin/env python
#coding:utf-8
L=[]
def longStr(*args): ##一个*号
for i in args:
L.append(len(i))
return args[L.index(max(L))]
t=("abc","kangamin","wo")
print longStr(*t) ##调用传参也是一个*号
####可变关键字参数#####
#!/usr/bin/env python
#coding:utf-8
def userInfo(name,age,**other): ##两个*号 类型是一个字典
print name,age,other
def hello(*args):
for i in args:
print i
dic={"kang":"amin","zhang":"luyi"}
userInfo("yiyi",5,**dic) ##调用的时候也是两个*号
L=[1,2,3,4]
hello(*L)
####lambda########
#!/usr/bin/env python
#coding=utf-8
num1=int(raw_input("input first number:"))
iron=raw_input("char + - * /:")
num2=int(raw_input("input second number:"))
dic={"+":lambda x,y:x+y,
"-":lambda x,y:x-y,
"*":lambda x,y:x*y,
"/":lambda x,y:x/y}
print dic[iron](num1,num2)
~
#!/bin/usr/env python
#coding = utf-8
from time import time,ctime
import getpass
db={"kang":["amin",0],"yiyi":["bushe",0]}
def menu():
while True:
print '''
(A)dd user
(L)ogin user
(D)elete user
(S)how user
(E)xit
'''
choice=raw_input("please input your choice:").lower()
if choice == "d":
deluser()
elif choice == "a":
adduser()
elif choice == "l":
login()
elif choice == "s":
showuser()
elif choice == "e":
exit(0)
else:
print "input error!"
def adduser():
while True:
name=raw_input("please input username:")
pwd=getpass.getpass("please input password:")
if name in db.keys():
print "the %s user is exist!" %name
continue
else:
break
logintime=time()
db[name]=[pwd,logintime]
def login():
for i in range(3):
name=raw_input("login username:")
pwd=getpass.getpass("passwd:")
if name in db.keys():
if pwd==db.get(name)[0]:
print "login success"
print "the last login time in at:",ctime(db[name][1])
current=time()
delta=current-db[name][1]
if delta<=14400:
print "You already logged in 4 hours period!"
else:
logintime=time()
db[name][1]=logintime
break
else:
print "login incorrect!"
else:
print "login incorrect"
def deluser():
name=raw_input("please input delete username:")
if name in db.keys():
db.pop(name)
print "the username delete success!"
else:
print "the username is not exist!"
def showuser():
for k,v in db.items():
print "name=%s pass=%s" %(k,v)
menu()
######函数#########
def abs(x)
if x>0:
print x
else:
print -x
def abs(x)
if x>0:
return x
else:
return -x
当你print abs(-10)的时候 如果没有返回值的话他会返回一个NOne 如果有返回值的话他会返回返回值
python中函数还可以返回多个返回值 有多个返回值的时候他返回的会是一个元组,然后我们可以对返回的元组进行操作
def f():
a=1
b=2
c=3
return a,b,c
当调用f()的时候
In [4]: f()
Out[4]: (1, 2, 3)
In [5]: a,b,c=f()
In [6]: print a
1
In [7]: print b
2
In [8]: print c
3
In [26]: def power(x,y):
....: return x**y
In [27]: power(2,3)
Out[27]: 8
###可变参数###
#!/usr/bin/env python
#coding:utf-8
def add(*args):
print type(args) ##类型是一个元组
sum=0
for i in args:
sum+=i
return sum
L=[1,2,3,4]
print add(*L)
assert add(1,2)==3 ##断言hello(1,2)的返回值是不是3
[root@foundation14 code]# python args.py
<type 'tuple'>
10
找出最长字符串
#/usr/bin/env python
#coding:utf-8
L=[]
def longStr(*args): ##一个*号
for i in args:
L.append(len(i))
return args[L.index(max(L))]
t=("abc","kangamin","wo")
print longStr(*t) ##调用传参也是一个*号
####可变关键字参数#####
#!/usr/bin/env python
#coding:utf-8
def userInfo(name,age,**other): ##两个*号 类型是一个字典
print name,age,other
def hello(*args):
for i in args:
print i
dic={"kang":"amin","zhang":"luyi"}
userInfo("yiyi",5,**dic) ##调用的时候也是两个*号
L=[1,2,3,4]
hello(*L)
####lambda########
#!/usr/bin/env python
#coding=utf-8
num1=int(raw_input("input first number:"))
iron=raw_input("char + - * /:")
num2=int(raw_input("input second number:"))
dic={"+":lambda x,y:x+y,
"-":lambda x,y:x-y,
"*":lambda x,y:x*y,
"/":lambda x,y:x/y}
print dic[iron](num1,num2)
~