7-5(a)
import time as t
db = {}
def newuser():
prompt = 'login desired: '
while True:
name = raw_input(prompt).lower()
if name in db:
prompt = 'name taken, try another: '
else:
break
pwd = raw_input('passwd: ')
db[name] = [pwd, 0]
def olduser():
name = raw_input('login: ').lower()
pwd = raw_input('passwd: ')
passwd = db.get(name, [None, 0])[0]
logintime = t.time()
blogintime = db.get(name, [None, 0])[1]
if logintime - blogintime <= 14400 and blogintime != 0:
return 'You already logged in at: <%f>' % blogintime
if passwd == pwd:
print 'welcome back', name
db[name][1] = logintime
else:
return 'login incorrect'
def manage():
prompt = """
(D)elete an account
(P)rint all name and password
Enter your choice: """
try:
choice = raw_input(prompt).strip()[0].lower()
except (EOFError, KeyboardInterrupt), e:
print e
return
print "You picked: [%s]" % choice
if choice == 'd':
name = raw_input("name you want to delete: ").lower()
del db[name]
else:
for i in db:
print i, db[i][0:2],
def showmenu():
prompt = """
(N)ew User Login
(E)xisting User Login
(M)anage User dictionary
(Q)uit
Enter choice: """
done = False
while not done:
chosen = False
while not chosen:
try:
choice = raw_input(prompt).strip()[0].lower()
except (EOFError, KeyboardInterrupt):
choice = 'q'
print '\nYou picked: [%s]' % choice
if choice not in 'neqm':
print 'invalid option, try again'
else:
chosen = True
if choice == 'q':
done = True
if choice == 'n':
newuser()
if choice == 'e':
olduser()
if choice == 'm':
manage()
if __name__ == '__main__':
showmenu()
7-6
def stock():
# create a list of the data
prompt = """
how many times you'll input?
(input 4 data in a line at one time)
(all we want is a digit)
Enter: """
num = int(raw_input(prompt))
datalist = [raw_input('your data(separated by a blank): ').split()
for i in range(num)]
# show the data
print '\nYour data:'
print '-'*5 + 'Stock Market Show-' + '-Your Stocks-' + '-Buying Price-' + '-Current Price-' + '-'*5
for line in datalist:
print '{0:5}{1[0]:^17} {1[1]:^11} {1[2]:^12} {1[3]:^13}{0:5}'.format(' ', line)
# sort the list
prompt = """
which series of data do you want to sort?
s: Stock Market Show
y: Your Stocks
b: Buying Price
c: Current Price
(The values of the data cannot appear more than one time)
Enter your choice: """
ziplist = zip(*datalist)
choice = raw_input(prompt)
lookup = {'s': ziplist[0], 'y': ziplist[1], 'b': ziplist[2], 'c': ziplist[3]}
if choice in 'sy':
print sorted(lookup[choice])
else:
newlist = sorted([int(i) for i in lookup[choice]])
print newlist
# create the dictionary
resultdic = {lookup[choice][i]: datalist[i] for i in range(num)}
print resultdic
stock()
7-7
dic1 = {'a': 1, 'b': 2, 'c': 3}
dic2 = {}
for i in dic1:
dic2[dic1[i]] = i
print dic2
print {dic1[x]: x for x in dic1}
7-8
def employee():
# input the data
print '''
Two things you shouldo:
1: Input the information of employees
(like John-55)
2: Sort the information
'''
num = int(raw_input('how many people(input a digit): '))
information = [raw_input('>: ').split('-') for i in range(num)]
# sort the data
prompt = '''
sorted by:
a: name
b: number
Enter your choice: '''
choice = raw_input(prompt)
ziplist = zip(*information)
lookupa = {alist[0]: alist for alist in information}
lookupb = {alist[1]: alist for alist in information}
if choice == 'a':
newtable = [lookupa[sorted(ziplist[0])[i]]
for i in range(num)]
else:
newtable = [lookupb[str(sorted([int(j) for j in ziplist[1]])[i])]
for i in range(num)]
# print the data
print '-'*10 + 'information'.capitalize() + '-'*10
print '{0: ^15}{1: ^16}'.format('name', 'number')
for line in newtable:
print '{0[0]: ^15}{0[1]: ^16}'.format(line)
employee()