Python核心編程-第七章-習題(一)

1.   字典的update()方法用來將兩個字典合併到一塊兒python

>>> dict1 = {'a':1, 'b':2, 'c': 3}
>>> dict2 = {'d':4, 'e': 5}
>>> dict1.update(dict2)
>>> dict1 
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}

2.  可哈希的數據類型才能夠做爲字典的鍵。字符串和數字能夠,列表、字典等類型不能夠
app

3.  acode

# -*- coding:utf-8 -*-

dict1 = {'name': 'Jer', 'sex': 'male', 'age': '24', 'computer': 'Lenovo'}
for eachkey in sorted(dict1):
    print eachkey

    bip

# -*- coding:utf-8 -*-

dict1 = {'name': 'Jer', 'sex': 'male', 'age': '24', 'computer': 'Lenovo'}
for eachkey in sorted(dict1):
    print eachkey, dict1[eachkey]

    cutf-8

# -*- coding:utf-8 -*-

dict1 = {'name': 'Jer', 'sex': 'male', 'age': '24', 'computer': 'Lenovo'}
for eachkey, eachvalue in sorted(dict1.items(), key = lambda x: x[1]):
    print eachkey, eachvalue

4.  字符串

# -*- coding:utf-8 -*-

import string

numlist = []
strlist = []
for i in range(1, 5):
    numlist.append(i)

for i in range(3, 13, 3):
    str1 = string.lowercase[i-3:i]
    strlist.append(str1)
    
dict1 = dict(zip(numlist, strlist))

print dict1

5.  a.get

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import datetime
 
db = {}
timedb = {}
 
def newuser():
    prompt = u'註冊賬號:'
    while True:
        name = raw_input(prompt.encode('gbk'))
        if name in db:
            prompt = u'賬號已被註冊,請更換一個:'
            continue
        else:
            break
    pwd = raw_input(u'輸入密碼:'.encode('gbk'))
    db[name] = pwd
     
def olduser():
    name = raw_input(u'登錄帳號:'.encode('gbk'))
    pwd = raw_input(u'登陸密碼:'.encode('gbk'))
    passwd = db.get(name)
    if passwd == pwd:
        nowtime = datetime.datetime.now()        
        if timedb.get(name):
            timedelta = nowtime - timedb.get(name)
            if timedelta.seconds <= 14400 and timedelta.days <= 0:
                print u'您已經登錄,登錄時間爲:%s' % timedb[name].strftime('%Y-%m-%d %H:%M:%S')
        print u'登陸成功,歡迎你',name
        timedb[name] = nowtime
    else:
        print u"登陸失敗,賬號或密碼錯誤"
         
def showmenu():
    prompt = u"""
(N)新用戶註冊
(E)已有帳戶登陸
(Q)退出
 
請輸入相應字母作出選擇:"""
 
    done = False
    while not done:
         
        chosen = False
        while not chosen:
            try:
                choice = raw_input(prompt.encode('gbk')).strip()[0].lower()
            except (EOFError, KeyboardInterrupt, IndexError):
                choice = 'q'
            print u'\n你選擇了:【%s】' % choice
            if choice not in 'neq':
                print u"輸入錯誤,請重試"
            else:
                chosen = True
         
        if choice == 'q':
            done = True
        if choice == 'n':
            newuser()
        if choice == 'e':
            olduser()
             
if __name__ == '__main__':
    showmenu()

   b.input

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import datetime
 
db = {}
timedb = {}
 
def newuser():
    prompt = u'註冊賬號:'
    while True:
        name = raw_input(prompt.encode('gbk'))
        if name in db:
            prompt = u'賬號已被註冊,請更換一個:'
            continue
        else:
            break
    pwd = raw_input(u'輸入密碼:'.encode('gbk'))
    db[name] = pwd
     
def olduser():
    name = raw_input(u'登錄帳號:'.encode('gbk'))
    pwd = raw_input(u'登陸密碼:'.encode('gbk'))
    passwd = db.get(name)
    if passwd == pwd:
        nowtime = datetime.datetime.now()        
        if timedb.get(name):
            timedelta = nowtime - timedb.get(name)
            if timedelta.seconds <= 14400 and timedelta.days <= 0:
                print u'您已經登錄,登錄時間爲:%s' % timedb[name].strftime('%Y-%m-%d %H:%M:%S')
        print u'登陸成功,歡迎你',name
        timedb[name] = nowtime
    else:
        print u"登陸失敗,賬號或密碼錯誤"
         
def admin():
    prompt = u"""
(D)刪除一個用戶
(V)顯示全部用戶賬號及密碼清單
(Q)退出管理員菜單

請輸入相應字母作出選擇:"""

    done = False
    while not done:
         
        chosen = False
        while not chosen:
            try:
                choice = raw_input(prompt.encode('gbk')).strip()[0].lower()
            except (EOFError, KeyboardInterrupt, IndexError):
                choice = 'q'
            if choice not in 'dvq':
                print u"輸入錯誤,請重試"
            else:
                chosen = True
        
        if choice == 'q':
            done = True
        if choice == 'd':
            to_pop = raw_input(u'請輸入要刪除的用戶賬號'.encode('gbk')).strip()
            if not db.get(to_pop):
                print u'無此帳戶,請查實!'
            else:
                db.pop(to_pop)
        if choice == 'v':
            print u'全部用戶帳戶密碼以下:'
            for eachkey, eachvalue in db.items():
                print u'帳戶:%s, 密碼:%s' % (eachkey, eachvalue)
    
def showmenu():
    prompt = u"""
(A)管理員登陸
(N)新用戶註冊
(E)已有帳戶登陸
(Q)退出
 
請輸入相應字母作出選擇:"""
 
    done = False
    while not done:
         
        chosen = False
        while not chosen:
            try:
                choice = raw_input(prompt.encode('gbk')).strip()[0].lower()
            except (EOFError, KeyboardInterrupt, IndexError):
                choice = 'q'
            print u'\n你選擇了:【%s】' % choice
            if choice not in 'aneq':
                print u"輸入錯誤,請重試"
            else:
                chosen = True
         
        if choice == 'q':
            done = True
        if choice == 'n':
            newuser()
        if choice == 'e':
            olduser()
        if choice == 'a':
            admin()
             
if __name__ == '__main__':
    showmenu()

    c.string

    d.
it

    e.

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import datetime

db = {}
timedb = {}
 
def newuser():
    prompt = u'註冊賬號:'
    while True:
        name = raw_input(prompt.encode('gbk'))
        if name.lower() in db:
            prompt = u'賬號已被註冊,請更換一個:'
            continue
        else:
            break
    pwd = raw_input(u'輸入密碼:'.encode('gbk'))
    name = name.lower()
    db[name] = pwd
     
def olduser():
    name = raw_input(u'登錄帳號:'.encode('gbk')).lower()
    pwd = raw_input(u'登陸密碼:'.encode('gbk'))
    passwd = db.get(name)
    if passwd == pwd:
        nowtime = datetime.datetime.now()        
        if timedb.get(name):
            timedelta = nowtime - timedb.get(name)
            if timedelta.seconds <= 14400 and timedelta.days <= 0:
                print u'您已經登錄,登錄時間爲:%s' % timedb[name].strftime('%Y-%m-%d %H:%M:%S')
        print u'登陸成功,歡迎你',name
        timedb[name] = nowtime
    else:
        print u"登陸失敗,賬號或密碼錯誤"
         
def admin():
    prompt = u"""
(D)刪除一個用戶
(V)顯示全部用戶賬號及密碼清單
(Q)退出管理員菜單

請輸入相應字母作出選擇:"""

    done = False
    while not done:
         
        chosen = False
        while not chosen:
            try:
                choice = raw_input(prompt.encode('gbk')).strip()[0].lower()
            except (EOFError, KeyboardInterrupt, IndexError):
                choice = 'q'
            if choice not in 'dvq':
                print u"輸入錯誤,請重試"
            else:
                chosen = True
        
        if choice == 'q':
            done = True
        if choice == 'd':
            to_pop = raw_input(u'請輸入要刪除的用戶賬號'.encode('gbk')).strip()
            if not db.get(to_pop):
                print u'無此帳戶,請查實!'
            else:
                db.pop(to_pop)
        if choice == 'v':
            print u'全部用戶帳戶密碼以下:'
            for eachkey, eachvalue in db.items():
                print u'帳戶:%s, 密碼:%s' % (eachkey, eachvalue)
    
def showmenu():
    prompt = u"""
(A)管理員登陸
(N)新用戶註冊
(E)已有帳戶登陸
(Q)退出
 
請輸入相應字母作出選擇:"""
 
    done = False
    while not done:
         
        chosen = False
        while not chosen:
            try:
                choice = raw_input(prompt.encode('gbk')).strip()[0].lower()
            except (EOFError, KeyboardInterrupt, IndexError):
                choice = 'q'
            print u'\n你選擇了:【%s】' % choice
            if choice not in 'aneq':
                print u"輸入錯誤,請重試"
            else:
                chosen = True
         
        if choice == 'q':
            done = True
        if choice == 'n':
            newuser()
        if choice == 'e':
            olduser()
        if choice == 'a':
            admin()
             
if __name__ == '__main__':
    showmenu()

    f.

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import datetime

db = {}
timedb = {}
 
def newuser():
    prompt = u'註冊賬號:'
    while True:
        name = raw_input(prompt.encode('gbk'))
        if name.lower() in db:
            prompt = u'賬號已被註冊,請更換一個:'
            continue
        elif not name.isalnum():
            prompt = u'帳號中不容許符合和空白符,請更換一個:'
            continue
        else:
            break
    pwd = raw_input(u'輸入密碼:'.encode('gbk'))
    name = name.lower()
    db[name] = pwd
     
def olduser():
    name = raw_input(u'登錄帳號:'.encode('gbk')).lower()
    pwd = raw_input(u'登陸密碼:'.encode('gbk'))
    passwd = db.get(name)
    if passwd == pwd:
        nowtime = datetime.datetime.now()        
        if timedb.get(name):
            timedelta = nowtime - timedb.get(name)
            if timedelta.seconds <= 14400 and timedelta.days <= 0:
                print u'您已經登錄,登錄時間爲:%s' % timedb[name].strftime('%Y-%m-%d %H:%M:%S')
        print u'登陸成功,歡迎你',name
        timedb[name] = nowtime
    else:
        print u"登陸失敗,賬號或密碼錯誤"
         
def admin():
    prompt = u"""
(D)刪除一個用戶
(V)顯示全部用戶賬號及密碼清單
(Q)退出管理員菜單

請輸入相應字母作出選擇:"""

    done = False
    while not done:
         
        chosen = False
        while not chosen:
            try:
                choice = raw_input(prompt.encode('gbk')).strip()[0].lower()
            except (EOFError, KeyboardInterrupt, IndexError):
                choice = 'q'
            if choice not in 'dvq':
                print u"輸入錯誤,請重試"
            else:
                chosen = True
        
        if choice == 'q':
            done = True
        if choice == 'd':
            to_pop = raw_input(u'請輸入要刪除的用戶賬號'.encode('gbk')).strip()
            if not db.get(to_pop):
                print u'無此帳戶,請查實!'
            else:
                db.pop(to_pop)
        if choice == 'v':
            print u'全部用戶帳戶密碼以下:'
            for eachkey, eachvalue in db.items():
                print u'帳戶:%s, 密碼:%s' % (eachkey, eachvalue)
    
def showmenu():
    prompt = u"""
(A)管理員登陸
(N)新用戶註冊
(E)已有帳戶登陸
(Q)退出
 
請輸入相應字母作出選擇:"""
 
    done = False
    while not done:
         
        chosen = False
        while not chosen:
            try:
                choice = raw_input(prompt.encode('gbk')).strip()[0].lower()
            except (EOFError, KeyboardInterrupt, IndexError):
                choice = 'q'
            print u'\n你選擇了:【%s】' % choice
            if choice not in 'aneq':
                print u"輸入錯誤,請重試"
            else:
                chosen = True
         
        if choice == 'q':
            done = True
        if choice == 'n':
            newuser()
        if choice == 'e':
            olduser()
        if choice == 'a':
            admin()
             
if __name__ == '__main__':
    showmenu()

    g.

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import datetime

db = {}
timedb = {}
 
def newuser():
    prompt = u'註冊賬號:'
    while True:
        name = raw_input(prompt.encode('gbk'))
        if name.lower() in db:
            prompt = u'賬號已被註冊,請更換一個:'
            continue
        elif not name.isalnum():
            prompt = u'帳號中不容許符合和空白符,請更換一個:'
            continue
        else:
            break
    pwd = raw_input(u'輸入密碼:'.encode('gbk'))
    name = name.lower()
    db[name] = pwd
     
def olduser():
    name = raw_input(u'登錄帳號:'.encode('gbk')).lower()
    pwd = raw_input(u'登陸密碼:'.encode('gbk'))
    passwd = db.get(name)
    if passwd == pwd:
        nowtime = datetime.datetime.now()        
        if timedb.get(name):
            timedelta = nowtime - timedb.get(name)
            if timedelta.seconds <= 14400 and timedelta.days <= 0:
                print u'您已經登錄,登錄時間爲:%s' % timedb[name].strftime('%Y-%m-%d %H:%M:%S')
        print u'登陸成功,歡迎你',name
        timedb[name] = nowtime
    else:
        usrchoice = raw_input(u"登陸失敗,賬號或密碼錯誤。若需註冊新賬號,請輸入1,繼續登陸請輸入2".encode('gbk'))
        if usrchoice == '1':
            newuser()
        else:
            olduser()
def admin():
    prompt = u"""
(D)刪除一個用戶
(V)顯示全部用戶賬號及密碼清單
(Q)退出管理員菜單

請輸入相應字母作出選擇:"""

    done = False
    while not done:
         
        chosen = False
        while not chosen:
            try:
                choice = raw_input(prompt.encode('gbk')).strip()[0].lower()
            except (EOFError, KeyboardInterrupt, IndexError):
                choice = 'q'
            if choice not in 'dvq':
                print u"輸入錯誤,請重試"
            else:
                chosen = True
        
        if choice == 'q':
            done = True
        if choice == 'd':
            to_pop = raw_input(u'請輸入要刪除的用戶賬號'.encode('gbk')).strip()
            if not db.get(to_pop):
                print u'無此帳戶,請查實!'
            else:
                db.pop(to_pop)
        if choice == 'v':
            print u'全部用戶帳戶密碼以下:'
            for eachkey, eachvalue in db.items():
                print u'帳戶:%s, 密碼:%s' % (eachkey, eachvalue)
    
def showmenu():
    prompt = u"""
(A)管理員登陸
(N)新用戶註冊
(E)已有帳戶登陸
(Q)退出
 
請輸入相應字母作出選擇:"""
 
    done = False
    while not done:
         
        chosen = False
        while not chosen:
            try:
                choice = raw_input(prompt.encode('gbk')).strip()[0].lower()
            except (EOFError, KeyboardInterrupt, IndexError):
                choice = 'q'
            print u'\n你選擇了:【%s】' % choice
            if choice not in 'aneq':
                print u"輸入錯誤,請重試"
            else:
                chosen = True
         
        if choice == 'q':
            done = True
        if choice == 'n':
            newuser()
        if choice == 'e':
            olduser()
        if choice == 'a':
            admin()
             
if __name__ == '__main__':
    showmenu()
相關文章
相關標籤/搜索