我討厭在同一天出現另一個問題,更不用說兩個問題了,但現在我需要一種方法來註冊和刪除帳戶。下面是整個腳本:註冊和刪除帳戶
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."
請忽略許多錯誤和複雜的代碼行可以用一個代替。我只需要一種方法來註冊和刪除賬戶(又名字典條目)。
關於你決定爲我重寫整個腳本的機會很多。
+1抽出時間,你是個好人=)。您可以使用'getpass'來獲取密碼以防止被回顯。而imho'q'應該是退出選項。但除此之外,不錯的代碼! – katrielalex 2010-12-20 03:48:19
@katrielalex:很好的建議!我相應地修改了代碼。 – 2010-12-20 04:22:14
允許'{}',而不是'{1}',格式(x)'只適用於自Python 3.1以來的版本。 – 2010-12-20 04:34:11