I hate to come with another problem on the same day, let alone two, but now I need a way to register and delete accounts. Here's the entire script:
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."
Please ignore the numerous errors and complex lines of code that could be replaced with one. I only need a way to register and delete accounts (aka the dictionary entries).
On the off chance that you decide to rewrite the ENTIRE script for me, much obliged.
Here's a pretty complete rewrite:
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()
Well, you're using a dictionary to store accounts. To add an item to a dictionary, you do:
accounts["newusername"] = "newpassword"
And to remove an item, it looks like:
del accounts["usernametodelete"]
(But I suggest you do spend some time working on your "complex lines of code that could be replaced with one"--it will make the program much easier to read. For example, where you have the complex set of if/else statements setting option1 and option2, why not just use something like:
if choice in accounts and choice2 == accounts[choice]:
Fix your code, there are lots of errors.
I am assuming this is what you want to do.
>>> 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
If not, comment and I will update.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.