2010-12-19 192 views
0

我討厭在同一天出現另一個問題,更不用說兩個問題了,但現在我需要一種方法來註冊和刪除帳戶。下面是整個腳本:註冊和刪除帳戶

accounts={'Robrajow':'password', 'Hacker':'hack', 'Noob':'lololol1'} 
option1=0 
option2=0 
loop=1 
while loop==1: 
    print "Welcome to Telemology! You can login, register, delete an account, or exit." 
    loginchoice = raw_input("What would you like to do? ") 
    if loginchoice=='login': 
     choice = raw_input("What is your username? ") 
     choice2 = raw_input("What is your password? ") 
     if (choice in accounts): 
      option1=1 
     else: 
      option1=0 
     if (choice2 == accounts[choice]): 
      option2=1 
     else 
      option2=0 
     if option1==1 and option2==1: 
      print "Welcome to Telemology,", choice 
     else: 
      print "The username or password you entered is incorrect. Please try again or register." 
    elif loginchoice=='register': 
     newuser=raw_input("What is your new username? ") 
     if (newuser in accounts): 
      newuser=raw_input("That account name is already taken. Please enter another: ") 
     newpass=raw_input("What is your new password? ") 
     print newuser,", welcome to Telemology!" 
     print newpass, "is your new password!" 
     print "Write it down and hide it!" 
    elif loginchoice=='delete': 
     unsure=raw_input("Do you really want to delete your account? It will never return! ") 
     if unsure=='yes': 
      choice = raw_input("What is your username? ") 
      choice2 = raw_input("What is your password? ") 
      if choice in accounts: 
       option1=1 
      else: 
       option1=0 
      if choice2 in password: 
       option2=1 
      else: 
       option2=0 
      if option1==1 and option2==1: 
       print "Goodbye forever,", choice 
      else: 
       print "The username or password you entered is incorrect." 
     elif unsure=='no': 
      print "I hoped so." 
     else: 
      print "Invalid input." 
    elif loginchoice==exit: 
     print "Goodbye!" 
     loop=0 
    else: 
     print "What? You can only input login, delete, register, or exit." 

請忽略許多錯誤和複雜的代碼行可以用一個代替。我只需要一種方法來註冊和刪除賬戶(又名字典條目)。

關於你決定爲我重寫整個腳本的機會很多。

回答

3

那麼,你正在使用字典來存儲帳戶。要將項目添加到字典,你這樣做:

accounts["newusername"] = "newpassword" 

,並刪除一個項目,它看起來像:

del accounts["usernametodelete"] 

(不過我建議你做花一些時間在你的「復行工作的代碼可以用一個代替「 - 這會使得程序更容易閱讀,例如,如果你有複雜的if/else語句設置選項1和選項2,爲什麼不使用類似於:

if choice in accounts and choice2 == accounts[choice]: 
0

修復你的代碼,有很多錯誤。

我假設這是你想要做的。

>>> if username in accounts: # if the username is in accounts 
    if accounts[username] == password: # check whether the password is same 
     del accounts[username] # if it is, delete the user 
    else: 
     print 'Password invalid' # else tell the user the password is invalid 

如果不是,評論和我會更新。

4

這是一個相當完整的重寫:

import hashlib 
import getpass 

class Auth(object): 
    class UserNameTakenError(Exception): 
     pass 

    def __init__(self, userDict): 
     self._users = {name:self.hash(pwd) for name,pwd in userDict.iteritems()} 

    def hash(self, pw): 
     return hashlib.sha256(pw+"&@#salt)(string)846!^").digest() 

    def validate(self, name, pwd): 
     return (name in self._users) and (self._users[name]==self.hash(pwd)) 

    def create(self, name, pwd): 
     if name in self._users: 
      raise Auth.UserNameTakenError() 
     else: 
      self._users[name] = self.hash(pwd) 

    def delete(self, name, pwd): 
     if self.validate(name, pwd): 
      del self._users[name] 

def getYes(msg): 
    return raw_input(msg).lower() in ('y','yes') 

class App(object): 
    def __init__(self): 
     self.auth = Auth({'Robrajow':'password', 'Hacker':'hack', 'Noob':'lololol1'}) 
     self.actions = { 
      'login': self.doLogin, 
      'l':  self.doLogin, 
      'register': self.doRegister, 
      'r':  self.doRegister, 
      'delete': self.doDelete, 
      'd':  self.doDelete, 
      'exit':  self.doExit, 
      'e':  self.doExit, 
      'x':  self.doExit, 
      'q':  self.doExit 
     } 

    def welcome(self): 
     return raw_input("\nWelcome to Telemology! You can Login, Register, Delete an account, or Exit.\nWhat would you like to do? ") 

    def doLogin(self): 
     name = raw_input("What is your username? ") 
     pwd = getpass.getpass("What is your password? ") 
     if self.auth.validate(name, pwd): 
      print "Welcome to Telemology, {0}".format(name) 
     else: 
      print "The username or password you entered is incorrect. Please try again or register." 
     return False 

    def doRegister(self): 
     name = raw_input("What is your new username? ") 
     pwd = getpass.getpass("What is your new password? ") 
     try: 
      self.auth.create(name, pwd) 
      print "{0}, welcome to Telemology!".format(name) 
     except Auth.UserNameTakenError: 
      print "That account name is already taken. Please try again." 
     return False 

    def doDelete(self): 
     name = raw_input("What is your username? ") 
     pwd = getpass.getpass("What is your password? ") 
     if self.auth.validate(name, pwd): 
      if getYes("Do you really want to delete your account? It will never return! "): 
       self.auth.delete(name, pwd) 
       print "Goodbye forever, {0}".format(name) 
      else: 
       print "I hoped so." 
     else: 
      print "The username or password you entered is incorrect." 
     return False 

    def doExit(self): 
     print "Goodbye!" 
     return True 

    def run(self): 
     while True: 
      act = self.welcome().lower() 
      if act in self.actions: 
       if self.actions[act](): 
        break 
      else: 
       print "What? You can only input login, delete, register, or exit." 

def main(): 
    myapp = App() 
    myapp.run() 

if __name__=="__main__": 
    main() 
+1

+1抽出時間,你是個好人=)。您可以使用'getpass'來獲取密碼以防止被回顯。而imho'q'應該是退出選項。但除此之外,不錯的代碼! – katrielalex 2010-12-20 03:48:19

+0

@katrielalex:很好的建議!我相應地修改了代碼。 – 2010-12-20 04:22:14

+0

允許'{}',而不是'{1}',格式(x)'只適用於自Python 3.1以來的版本。 – 2010-12-20 04:34:11