File size: 1,517 Bytes
fa2a333
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import sqlite3

# First, we'll create a connection to the database
conn = sqlite3.connect("users.db")
cursor = conn.cursor()

# Now, we'll create a table to store the users and their corresponding passwords
cursor.execute("CREATE TABLE IF NOT EXISTS users (username text, password text)")

# Now, we'll define a function to handle signup
def signup():
  # Prompt the user to enter a username and password
  username = input("Enter a username: ")
  password = input("Enter a password: ")
  # Add the username and password to the users table
  cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
  conn.commit()
  print("Signup successful!")

# Next, we'll define a function to handle login
def login():
  # Prompt the user to enter their username and password
  username = input("Enter your username: ")
  password = input("Enter your password: ")
  # Check if the username and password match a set in the users table
  cursor.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
  if cursor.fetchone() is not None:
    print("Login successful!")
  else:
    print("Invalid username or password. Please try again.")

# Now, we'll prompt the user to choose between signup and login
while True:
  choice = input("Would you like to signup or login? (s/l) ")
  if choice == 's':
    signup()
  elif choice == 'l':
    login()
  else:
    print("Invalid choice. Please try again.")

# Finally, we'll close the connection to the database
conn.close()