Advertisement
nyapoleon_legionary

MainUserRegistPY

Dec 2nd, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.58 KB | None | 0 0
  1. import os
  2.  
  3. USER_DATABASE = "user-authenticationr.txt"
  4.  
  5. def initialize_database():
  6.     #bikin file database kalo belom ada di direktori (yang user-authentication.txt)
  7.     try:
  8.         with open(USER_DATABASE, "x") as file:
  9.             print("User database created successfully!")
  10.     except FileExistsError:
  11.         print("User database already exists.")
  12.  
  13.  
  14. def clear_database():
  15.     #fungsi kalo mau hapus bersih database nya
  16.     #(yang user-authentication.txt, tapi gak delete file fisiknya cuma delete isi nya)
  17.     confirmation = input("Yakin mau delete database? (ketik iya/no): ").strip().lower()
  18.     if confirmation == "iya":
  19.         with open(USER_DATABASE, "w") as file:
  20.             file.write("")  # string kosong biar isi filenya ilang semua
  21.         print("Dah dihapus ya, gak bisa balik!")
  22.     else:
  23.         print("Oh gak jadi dihapus? oke")
  24.  
  25. def regist_main():
  26.     Userna = input("Ketik username : ").strip()
  27.     Passw = input("ketik password: ").strip()
  28.    
  29.     database_created = False
  30.     # ngecek kalo database nya ada / ga
  31.     if not os.path.exists(USER_DATABASE):  # deklarasi file gak ada / belum dibuat
  32.         with open(USER_DATABASE, "x") as file:
  33.             database_created = True  # deklarasi kalo file nya ada & udah dibuat
  34.  
  35.     if database_created:
  36.     # Munculin pesan kalo database berhasil dibuat
  37.         print("User database created successfully!")
  38.  
  39.     # Bagian ngecek username
  40.     try:
  41.         with open(USER_DATABASE, "r") as file:
  42.             users = [line.split("|")[0] for line in file.readlines()]
  43.     except FileNotFoundError:
  44.         users = []
  45.  
  46.     # Ngecek kalo username nya udah ada biar gak double
  47.     if Userna in users:
  48.         print("Username already exists. Please try a different one.")
  49.         return
  50.  
  51.     # Append/nambahin username yang baru
  52.     with open(USER_DATABASE, "a") as file:
  53.         file.write(f"{Userna}|{Passw}\n")
  54.     print("Registration successful!")
  55.  
  56. def login_main():
  57.     # login biasa
  58.     Userne = input("Enter your Username: ").strip()
  59.     Passwo = input("Enter your Password: ").strip()
  60.  
  61.     try:
  62.         with open(USER_DATABASE, "r") as file:
  63.             userand = file.readlines()
  64.  
  65.         if not userand:
  66.             print("No users found. Start by registering first!")
  67.             return
  68.  
  69.         for line in userand:
  70.             username, password = line.strip().split("|")
  71.             if username == Userne:
  72.                 if password == Passwo:
  73.                     print("Login successful!")
  74.                     return
  75.                 else:
  76.                     print("Incorrect password!")
  77.                     return
  78.        
  79.         print("Username not found. Please register first!")
  80.  
  81.     except FileNotFoundError:
  82.         print("No data found. Start by registering first!")
  83.     except Exception as e:
  84.         print(f"An error occurred: {e}")
  85.  
  86. def main():
  87.     initialize_database()  # make sure kalo database nya ada
  88.  
  89.     while True:
  90.         print("\nLogin Panel       (ketik sesuai nomor buat masuk opsi)")
  91.         print("1. Register       (A append + X)")
  92.         print("2. Login          (R read)")
  93.         print("3. Clear Database (W write)")
  94.         print("4. Exit")
  95.         choice = input("Choose an option (1-4): ").strip()
  96.  
  97.         if choice == "1":
  98.             regist_main()
  99.         elif choice == "2":
  100.             login_main()
  101.         elif choice == "3":
  102.             clear_database()
  103.         elif choice == "4":
  104.             print("Goodbye!")
  105.             break
  106.         else:
  107.             print("Invalid choice. Please try again.")
  108.  
  109. if __name__ == "__main__":
  110.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement