我讨厌在同一天遇到另一个问题,更不用说两个了,但现在我需要一种注册和删除帐户的方法。这是整个脚本:
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."
请忽略可能被替换的众多错误和复杂的代码行。我只需要一种方法来注册和删除帐户(例如,字典条目)。
关于你决定为我重写整个脚本的机会,非常有必要。
答案 0 :(得分: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 :(得分:3)
嗯,你正在使用字典存储帐户。要将项添加到字典,请执行以下操作:
accounts["newusername"] = "newpassword"
要删除项目,它看起来像:
del accounts["usernametodelete"]
(但我建议你花点时间研究一下“可以用一个替换的复杂代码行” - 它会使程序更容易阅读。例如,你有复杂的if / else语句设置option1和option2,为什么不使用类似的东西:
if choice in accounts and choice2 == accounts[choice]:
答案 2 :(得分: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
如果没有,请评论,我会更新。