1.作业题
7–5. Userpw2.Py. 下面的问题和例题 7.1 中管理名字-密码的键值对数据的程序有关。
(A)修改那个脚本,使它能记录用户上次的登录日期和时间(用 Time 模块),并与用户密码一起
保存起来。程序的界面有要求用户输入用户名和密码的提示。无论户名是否成功登录,都应有提示,
在户名成功登录后,应更新相应用户的上次登录时间戳。如果本次登录与上次登录在时间上相差不
超过 4 个小时,则通知该用户: “You Already Logged In At: <Last_ Login_Timestamp>.”
(B) 添加一个“管理”菜单,其中有以下两项:(1)删除一个用户 (2)显示系统中所有用户的名
字和他们的密码的清单。
(C) 口令目前没有加密。请添加一段对口令加密的代码(请参考 Crypt, Rotor, 或其它加密模块)
(D) 为程序添加图形界面,例如,用 Tkinter 写。
(E) 要求用户名不区分大小写。
(F) 加强对用户名的限制,不允许符号和空白符。
(G)合并“新用户”和“老用户”两个选项。如果一个新用户试图用一个不存在的用户名登录,
询问该用户是否是新用户,如果回答是肯定的,就创建该帐户。否则,按照老用户的方式登录。\
脚本:
#!/usr/bin/env python
#coding:utf-8
import getpass
import time
def menu():
while True:
print'''
(A)dd
(L)ogin
(D)elete
(S)how
(E)xit
'''
choice = raw_input("please input your choice:").upper()
if choice == "A":
Adduser()
elif choice == "L":
Loginuser()
elif choice == "S":
Showuser()
elif choice == "D":
Deleteuser()
elif choice == "E" :
exit(0)
else :
print"your choice is an error input!"
def Adduser():
username = raw_input("please input username:")
password = getpass.getpass("please input passwd:")
if dic.has_key(username):
print("error:%s has exist!")%username
else:
dic[username]=[password,time.time()]
print "add successfully!"
def Loginuser():
for i in range(0,3):
username = raw_input("please input your username:")
password = getpass.getpass("please input password:")
if dic.has_key(username) and dic[username][0]==password:
print "login successful!"
if time.time()-dic[username][1]>14400:
print "Last login at:%s"%(time.ctime(dic[username][1]))
else :
print "you already login %s within 4 hours"%(time.ctime(dic[username][1]))
break
else:
print "error:username is not exist or password in wrong!"
print "you have %d chance"%(2-i)
def Showuser():
print "show user:"
for data in dic.items():
print "name:%s\tpass:%s"%(data[0],data[1][0])
def Deleteuser():
deluser = raw_input("please input delete user:")
if dic.has_key(deluser):
dic.pop(deluser)
print "%s delete success"%deluser
dic = {"fentiao":["hello",100.0]}
menu()
1.如何让列表中包含多个信息,即一个key值可以有多个value值:
In [1]: dic = {} //定义一个空的列表
In [2]: dic["fentiao"]=123 //给列表中添加值
In [3]: dic
Out[3]: {'fentiao': 123}
In [4]: dic["westos"]=["hello","world"] //在列表中添加“列表”的value值
In [5]: dic
Out[5]: {'fentiao': 123, 'westos': ['hello', 'world']}
In [6]: value = ["123","12345"] //定义新的列表
In [7]: dic["text"]=value //将新的列表添加在列表中
In [8]: dic
Out[8]: {'fentiao': 123, 'text': ['123', '12345'], 'westos': ['hello', 'world']}
In [12]: dic.values()[1] //显示列表中的“列表”信息
Out[12]: ['hello', 'world']
In [13]: dic.values()[1][0]
Out[13]: 'hello'
In [14]: dic.values()[1][1]
Out[14]: 'world'
2.定义函数中有无retun的区别
In [18]: def abs(x):
....: if x>0:
....: print x
....: else:
....: print -x
....:
In [19]: abs(-10)
10
In [21]: print abs(-10)
10
None //默认返回值为none
In [13]: def abs(x):
....: if x>0:
....: return x
....: else:
....: return -x
....:
In [14]: a = abs(-10)
In [15]: a
Out[15]: 10
In [17]: print abs(10) //没有none返回值
10
n [8]: def f ():
...: a = 1
...: b = 2
...: c = 3
...: return a,b,c //可以返回多个值
...:
In [9]: f()
Out[9]: (1, 2, 3) //返回多个值默认定义为元组
In [10]: d,e,f = f() //返回值可以重新赋给其他变量
In [11]: print d,e,f
1 2 3
乘方计算:
In [1]: def power(n,x):
...: if isinstance(n,int) and isinstance(x,int):
...: return n**x //乘方表示法
...:
In [2]: power (2,3)
Out[2]: 8
In [3]: power (3,3)
Out[3]: 27
In [4]: def power (x,n=2): //计算二次方
...: return x**n
...:
In [9]: def power(n=2,x): //将给定的放在前面会报错
...: return x**n
File "<ipython-input-9-6ce9109bcf8c>", line 1
def power(n=2,x):
SyntaxError: non-default argument follows default argument
In [10]: def info(name,age=2,address="xi'an"): //将相对固定的参数放在后面
....: print name,age,address
....:
3.如何结束函数中“end”的无限次循环:
In [9]: def list_end(l=[]):
...: l.append("end")
...: return l
...:
In [10]: list_end()
Out[10]: ['end']
In [11]: list_end()
Out[11]: ['end', 'end']
In [12]: list_end()
Out[12]: ['end', 'end', 'end']
(2)
In [2]: def list_end(l=[]):
...: l=["123"]
...: l.append("end")
...: return l
...:
In [3]: list_end()
Out[3]: ['123', 'end']
In [4]: list_end()
Out[4]: ['123', 'end']
(2)
In [30]: def list_end(L = None):
....: if L is None :
....: L = []
....: L.append("end")
....: return L
....:
In [32]: list_end()
Out[32]: ['end']
In [33]: list_end()
Out[33]: ['end']
4.可变长度参数的介绍和使用:
在我们有不定数目的或者额外集合的关键字的情况中, 参数被放入一个字典中,字典中键为参#!/usr/bin/env python
数名,值为相应的参数值。为什么一定要是字典呢?因为为每个参数-参数的名字和参数值--都是成
对给出---用字典来保存这些参数自然就最适合不过了。
(1)元祖可变参数:
累加器:
def add(*args):
print type(args) //输出类型为元祖
sum = 0
for i in args:
sum = sum + i
return sum
print add(1,2,3,4) //输出结果为10
L = [1,2,3,4,5]
print add(*L)
'''
L = [1,2,3,4,5]
print add(L[0],L[1],L[2],L[3],L[4])
'''
输出最大最小值
def fun(*args):
for i in args:
if not isinstance(i,int):
print "type is not int"
else:
return max(args),min(args)
print fun(1,2,3554,33,2,35)
print "max=%d,min=%d"%fun(1,44,33,4,522,555,463)
输入字符串,并输出最长的字符串:
法1:
#!/usr/bin/env python
def longstr(*args):
if len(args) == 0:
return "your have not input any"
strlen = []
for i in args:
strlen.append(len(i))
return args[strlen.index(max(strlen))] //在此之前是测量最长的字符串
li = []
string = raw_input("please input string('.'is over):") //定义变量
while string !='.':
li.append(string)
string = raw_input("please input string('.'is over):") //此段是字符串输入的过程
print longstr(*li)
~
法2:
#!/usr/bin/env python
#coding:utf-8
l = []
def longstr(*args):
for i in args:
l.append(len(i))
for i in args:
if len(i)==max(l):
print "the longest string is %s"%i
longstr("djiu","fajpokpdf")
5.索引:
In [2]: l = [5,7]
In [3]: t = ("hello","fentiao")
In [4]: max(l)
Out[4]: 7
In [5]: l.index(max(l))
Out[5]: 1
In [6]: t[l.index(max(l))]
Out[6]: 'fentiao'
6.可变关键字参数:
#!/usr/bin/env python
def info(name,age,**other):
print name,age,other
dic = {"gender":"male","kind":"company"}
info("westos",7,**dic)
6.lambda:相当于c语言中的case:
#!/usr/bin/env python
#coding:utf-8
from __future__ import division
x = raw_input("please input a number:")
y = raw_input("please input a number:")
c = raw_input("please input a operator:")
dic = {
"+":lambda x,y:int(x) + int(y),
"-":lambda x,y:int(x) - int(y),
"*":lambda x,y:int(x) * int(y),
"/":lambda x,y:int(x) / int(y)
}
for i in dic.keys():
if c == i:
print dic[i](x,y)
~