深入理解Python中字典的键的使用

时间:2025-01-31 08:32:47

db = {}

 

def newuser():

 

  prompt= 'please regist your name: '

 

  while True:

 

    name = raw_input(prompt)

 

    if db.has_key(name):

 

      prompt = 'name taken,try another: '

 

      continue

 

    else:

 

      break

 

  pwd = raw_input('passswd: ')

 

  db[name] = pwd

 

  print 'Newuser [%s] has added successfully!' %name

 

def olduser():

 

  name = raw_input('login: ')

 

  pwd = raw_input('passwd: ')

 

  passwd = (name)

 

  if passwd == pwd:

 

    print 'welcome back',name

 

  else:

 

    print 'login incorrect!'

 

 

def showmenu():

 

  prompt = """

 

(N)ew User Login

 

(E)xisting User Login

 

(Q)uit

 

Enter choice: """

 

  while True:

 

    try:

 

      choice = raw_input(prompt).strip()[0].lower()

 

      print '\nYou picked: [%s]' % choice

 

      if choice not in 'neq':

 

        print 'invalid option,please try again'

 

      if choice == 'n':

 

        newuser()

 

      if choice == 'e':

 

        olduser()

 

      if choice == 'q':

 

        break

 

    except(EOFError,KeyboardInterrupt):

 

      print 'invalid option,please try again'

 

 

if __name__ == '__main__':

 

  showmenu()