2017-07-06 41 views
0

您好我想作一個電影流媒體網站的登錄頁面,任何人都可以幫助我提高這個代碼,請Python的登錄頁面

我需要它來保存用戶名和密碼,當它已經做到了我需要的用戶名無效的人誰改掉再次用它做一個帳戶

username = input("Please enter your username : ") 
password = input("Please enter your password : ") 
print ("Greetings," , username, "Please re enter your password to confirm 
it") 
command = input("Please type a command :") 
if command == "log off": 
print("You have now been logged off again",username) 
username == "" 
password == "" 

username = input("Please enter your username : ") 
password = input("Please enter your password : ") 

while username != "username" and password != "password": 

    print (" Sorry username and password incorrect please re-enter for 
validation ") 
    username = input("Please enter your username : ") 
    password = input("Please enter your password : ") 

else: 
print ("Greetings," , username, "you are now logged in now with your new 
password") 

username = input("Please enter your username : ") 
password = input("Please enter your password : ") 
print ("Greetings," , username, "Please re enter your password to confirm 
it") 
command = input("Please type a command :") 
if command == "log off": 
print("You have now been logged off again",username) 
username == "" 
password == "" 

file = open("testfile.txt","w") 

file.write("Usernames and Passwords") 
file.write(("Username :") username + ("Password :") password) 


file.close() 
+1

的一些技巧,遵循**幹原則**,因爲你重複了很多代碼,它我也不清楚你在問什麼。你的具體問題是什麼? – Ludisposed

+0

我需要創建一個登錄頁面,將用戶名和密碼保存到一個文件中,然後您可以與該用戶登錄 – ThatSkeptic

+0

再次強調您的代碼格式不正確,'if'' else'語句沒有縮進 – Ludisposed

回答

1

我有種做了一個複雜的程序,我希望你能明白我在上面花費的時間。我認爲這個問題最好在codereview上提問,因爲它沒有提出特定的錯誤,而是如何改進你的代碼。

首先,我爲可重用性做了一些功能。這樣代碼就變得更加可讀,並且在整體上變得更有意義。其次,我添加了new_user()功能。我想提醒你,是不是codewriting服務,你應該覺得自己幸運,我有什麼好做

def log_in(): 
    username = input("Please enter your username : ") 
    password = input("Please enter your password : ") 

    with open('test_file.txt', 'r') as file: 
     for line in file: 
      if line == 'Username:{0}, Password:{1}'.format(username, password): 
       print ("Greetings," , username, "you are now logged in") 
       return True, username, password 
    print (" Sorry username and password incorrect please re-enter for validation ") 
    return False, '', '' 

def new_user(): 
    succes = False 
    while not succes: 
     new_user = input("Please enter your new username : ") 
     new_pass = input("Please enter your new password : ") 

     exists = False 
     with open("test_file.txt","r") as file: 
      for line in file: 
       if line.split(',')[0] == 'Username:'+new_user: 
        print ('Invalid username: {0} already exsist'.format(new_user)) 
        exists = True 

     if not exists: 
      with open("test_file.txt","a") as file: 
       file.write('Username:{0}, Password:{1}'.format(new_user, new_pass)) 
      succes = True 
    print ('You made a new user with username:{0} and password:{1}'.format(new_user, new_pass)) 

def main(): 
    command = username = password = '' 
    logged_in = False 
    while command != 'quit': 
     command = input('Please type a command: ') 
     if command == 'log in': 
      logged_in, username, passowrd = log_in() 
     if command == 'log out': 
      logged_in = False 
      username = passowrd = '' 
     if command == 'new user': 
      if not logged_in: 
       new_user() 
      else: 
       print ('First logout to make a new user') 

main()