亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 書上的例子,有些不解 [打印本頁]

作者: freeterman    時間: 2013-08-15 15:06
標(biāo)題: 書上的例子,有些不解
1 #!/usr/bin/env python
2
Edit By Vheavens
Edit By Vheavens
3 db = {}
4
5 def newuser():
6 prompt = 'login desired: '
7 while True:
8 name = raw_input(prompt)
9 if db.has_key(name):
10 prompt = 'name taken, try another: '
11 continue
12 else:
13 break
14 pwd = raw_input('passwd: ')
15 db[name] = pwd
16
17 def olduser():
18 name = raw_input('login: ')
19 pwd = raw_input('passwd: ')
20 passwd = db.get(name)
21 if passwd == pwd:
22 print 'welcome back', name
23 else:
24 print 'login incorrect'
25
26 def showmenu():
27 prompt = """
28 (N)ew User Login
29 (E)xisting User Login
30 (Q)uit
Example 7.1 Dictionary Example (userpw.py) (continued)
31
32 Enter choice: """
33
34 done = False
35 while not done:
36
37 chosen = False
38 while not chosen:
39 try:
40 choice = raw_input(prompt).strip()[0].lower()
41 except (EOFError, KeyboardInterrupt):
42 choice = 'q'
43 print '\nYou picked: [%s]' % choice
44 if choice not in 'neq':
45 print 'invalid option, try again'
46 else:
47 chosen = True
49 done = True
50 newuser()
51 olduser()
52
53 if __name__ == '__main__':
54 showmenu()


選擇e的時候 可以直接執(zhí)行olduser()函數(shù)嗎?
書上給出的是可以執(zhí)行,沒有看懂,請高手指點一下。
作者: 代號:軍刀    時間: 2013-08-15 17:16
這代碼一坨坨的,貼出來也不把他對整齊點
作者: a-plus    時間: 2013-08-15 18:36
樓主你這代碼真心的難看。
作者: ssfjhh    時間: 2013-08-16 11:44
這代碼僅僅是難看嗎?
lz難道不知道python的縮進代表什么嗎?
作者: freeterman    時間: 2013-08-18 14:02
知道縮進,這個是從pdf上復(fù)制過來的,沒有縮進的
只是這個例子和書上的結(jié)果不同,我已經(jīng)詳細看了,是缺少了判斷
作者: freeterman    時間: 2013-08-18 14:04
很對不起大家
#!/usr/bin/env python
db={}

def useradd():
    while True:
        user = raw_input("Enter username:")
        if user in db:
            print "username is exist,please input again"
            continue
        else:
            break
    pwd = raw_input("Enter user passwd:")
    db[user]=pwd
    showmenu()

def olduser():
    username = raw_input("Input username:")
    pwd = raw_input("Input password:")
    if username in db and pwd ==db[username]:
        print 'welcome %s' % username
    else:
        print 'input username or password is wrong'

def showmenu():
    prompt = """
    (N)ew User Login
    (E)xisting User Login
    (Q)uit
    Example 7.1 Dictionary Example (userpw.py) (continued)
    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 'neq':
                print 'invalid option, try again'
            else:
                chosen = True
                done = True
    useradd()
    olduser()
if __name__ == '__main__':
    showmenu()
作者: freeterman    時間: 2013-08-18 14:05
這個例子是python核心編程里面的
跑起來和例子的結(jié)果不同的




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2