免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5363 | 回复: 6
打印 上一主题 下一主题

书上的例子,有些不解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-08-15 15:06 |只看该作者 |倒序浏览
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的时候 可以直接执行olduser()函数吗?
书上给出的是可以执行,没有看懂,请高手指点一下。

论坛徽章:
13
丑牛
日期:2013-08-16 15:08:22技术图书徽章
日期:2013-11-26 10:13:40双鱼座
日期:2013-11-08 15:03:26戌狗
日期:2013-11-08 13:52:30技术图书徽章
日期:2013-11-05 14:06:30戌狗
日期:2013-10-31 11:45:42CU十二周年纪念徽章
日期:2013-10-24 15:41:34天秤座
日期:2013-10-11 14:55:08子鼠
日期:2013-09-26 19:36:35水瓶座
日期:2013-09-26 17:44:56午马
日期:2013-08-26 10:24:23丑牛
日期:2013-08-19 14:43:22
2 [报告]
发表于 2013-08-15 17:16 |只看该作者
这代码一坨坨的,贴出来也不把他对整齐点

论坛徽章:
0
3 [报告]
发表于 2013-08-15 18:36 |只看该作者
楼主你这代码真心的难看。

论坛徽章:
4
金牛座
日期:2013-10-11 16:12:50卯兔
日期:2014-07-31 09:17:19辰龙
日期:2014-08-08 09:28:02狮子座
日期:2014-09-14 20:32:05
4 [报告]
发表于 2013-08-16 11:44 |只看该作者
这代码仅仅是难看吗?
lz难道不知道python的缩进代表什么吗?

论坛徽章:
0
5 [报告]
发表于 2013-08-18 14:02 |只看该作者
知道缩进,这个是从pdf上复制过来的,没有缩进的
只是这个例子和书上的结果不同,我已经详细看了,是缺少了判断

论坛徽章:
0
6 [报告]
发表于 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()

论坛徽章:
0
7 [报告]
发表于 2013-08-18 14:05 |只看该作者
这个例子是python核心编程里面的
跑起来和例子的结果不同的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP