Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- USER_DATABASE = "user-authenticationr.txt"
- def initialize_database():
- #bikin file database kalo belom ada di direktori (yang user-authentication.txt)
- try:
- with open(USER_DATABASE, "x") as file:
- print("User database created successfully!")
- except FileExistsError:
- print("User database already exists.")
- def clear_database():
- #fungsi kalo mau hapus bersih database nya
- #(yang user-authentication.txt, tapi gak delete file fisiknya cuma delete isi nya)
- confirmation = input("Yakin mau delete database? (ketik iya/no): ").strip().lower()
- if confirmation == "iya":
- with open(USER_DATABASE, "w") as file:
- file.write("") # string kosong biar isi filenya ilang semua
- print("Dah dihapus ya, gak bisa balik!")
- else:
- print("Oh gak jadi dihapus? oke")
- def regist_main():
- Userna = input("Ketik username : ").strip()
- Passw = input("ketik password: ").strip()
- database_created = False
- # ngecek kalo database nya ada / ga
- if not os.path.exists(USER_DATABASE): # deklarasi file gak ada / belum dibuat
- with open(USER_DATABASE, "x") as file:
- database_created = True # deklarasi kalo file nya ada & udah dibuat
- if database_created:
- # Munculin pesan kalo database berhasil dibuat
- print("User database created successfully!")
- # Bagian ngecek username
- try:
- with open(USER_DATABASE, "r") as file:
- users = [line.split("|")[0] for line in file.readlines()]
- except FileNotFoundError:
- users = []
- # Ngecek kalo username nya udah ada biar gak double
- if Userna in users:
- print("Username already exists. Please try a different one.")
- return
- # Append/nambahin username yang baru
- with open(USER_DATABASE, "a") as file:
- file.write(f"{Userna}|{Passw}\n")
- print("Registration successful!")
- def login_main():
- # login biasa
- Userne = input("Enter your Username: ").strip()
- Passwo = input("Enter your Password: ").strip()
- try:
- with open(USER_DATABASE, "r") as file:
- userand = file.readlines()
- if not userand:
- print("No users found. Start by registering first!")
- return
- for line in userand:
- username, password = line.strip().split("|")
- if username == Userne:
- if password == Passwo:
- print("Login successful!")
- return
- else:
- print("Incorrect password!")
- return
- print("Username not found. Please register first!")
- except FileNotFoundError:
- print("No data found. Start by registering first!")
- except Exception as e:
- print(f"An error occurred: {e}")
- def main():
- initialize_database() # make sure kalo database nya ada
- while True:
- print("\nLogin Panel (ketik sesuai nomor buat masuk opsi)")
- print("1. Register (A append + X)")
- print("2. Login (R read)")
- print("3. Clear Database (W write)")
- print("4. Exit")
- choice = input("Choose an option (1-4): ").strip()
- if choice == "1":
- regist_main()
- elif choice == "2":
- login_main()
- elif choice == "3":
- clear_database()
- elif choice == "4":
- print("Goodbye!")
- break
- else:
- print("Invalid choice. Please try again.")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement