核心编程第七章上

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()

 

转载于:https://my.oschina.net/u/2519674/blog/657764

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值