Python实现的用户登录系统功能示例,看看吧!

来源:网络时间:2018-02-06 13:01:38

Python实现的用户登录系统功能示例,看看吧!

  本文实例讲述了Python实现的用户登录系统功能。分享给大家供大家参考,具体如下:

  有N,E,Q三个选择,若选择Q或者中断,则系统退出。若其他选项,则持续让用户选择。

?

1

2
3
4

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
db word">= {}
def newuser():
  prompt = 'login desired: '
  while 1:
    name = raw_input(prompt)
    if db.has_key(name):
      prompt = 'name taken, try another: '
      continue
    else:
      break
  pwd = raw_input('passwd: ')
  db[name] = pwd
def olduser():
  name = raw_input('login: ')
  pwd = raw_input('passwd: ')
  passwd = db.get(name)
  if passwd == pwd:
    pass
  else:
    print 'login incorrect'
    return
  print 'welcome back', name
def showmenu():##主函数名
  prompt = """
(N)ew User Login
(E)xisting User Login
(Q)uit
Enter choice: """
  done = 0 ##控制循环的控制器
  while not done:
    chosen = 0 ##控制循环的控制器
    while not chosen:
      try:
        choice = raw_input(prompt)[0]
      except (EOFError, KeyboardInterrupt):
        choice = 'q'
      print ' You picked: [%s]' % choice
      if choice not in 'neq':##非neq继续小循环
        print 'invalid menu option, try again'
      else:
        chosen = 1 ##跳出小循环
    if choice == 'q': done = 1 ##跳出大循环
    if choice == 'n': newuser()
    if choice == 'e': olduser()
if __name__ == '__main__':
  showmenu()

  运行结果:

Python实现的用户登录系统功能示例,看看吧!

文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站) 联系邮箱:rjfawu@163.com