Why is python ignoring my code for my username/password project?

33 views Asked by At
username = input("Please enter your username: ")

username0 = username
username1 = username.find(" ")
username2 = username.isdigit
username3 = len(username)

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

password1 = len(password)
password2 = password.find("!, @, #, $, %, ^, &, *, (, ), -, _, +, =, `, ~, {, [, }")
password3 = password.find("], |, :, ;, ', ,, ., ?, /")

if username1 == -1:
  pass
elif username1 >= 0:
  print("Your username can not contain spaces.")

breaker1 = 1

if username2:
  pass
elif not username2:
  print("Your username can not contain numbers.")

breaker2 = 2

if username3 > 12:
  print("Your username can not be more than 12 characters.")
elif username3 <= 12:
  pass

breaker3 = 3

if password1 > 12:
  print("Your password can not be more than 12 characters.")
elif password1 <= 12:
  pass
if password2 == -1:
  pass
elif password2 >= 0:
  print("Your password can not contain symbols.")
elif password3 == -1:
  pass
elif password3 >= 0:
  print("Your password can not contain symbols.")

password4 = password.replace("a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q", "*")
password5 = password.replace("r, s, t, u, v, w, x, y, z","*")



print("Your username is " + username + " and your password is " + password + ".")

print(username2)

I'm trying to make a code that can take your username and password, remember it, then be able to lock you out or let you in on whether or not you entered the right username and password.

I've tried rewriting certain areas, I've tried using the ai provided by the website I use to code, I tried breaking up the if statements (which is what "Breaker1 = 1" means, I thought maybe the strings of if statements was messing with the pass command). Keep in mind, I'm still relatively new to coding, I've only started learning python since about a week ago. I'm hoping someone could shed some insight on this? Thanks!

1

There are 1 answers

0
Ansh Agarwal On

Here is a bit of code with some useful functions that you can use. It allows you to register a username and password and store it in a text file in the same directory. That way it saves after your code stops running. This is not at all secure but as a beginner project it works.

def StoreUsernamePassword(username,password):

    #make sure there are no spaces 
    username = username.strip() 

    #make sure there are no numbers in the username 
    if any(char.isdigit() for char in username):
        print("Username cannot contain numbers")
        return
    
    #make sure username is less than 12 characters 
    if len(username) > 12:
        print("Username cannot be more than 12 characters")
        return
    
    #make sure password doesn't have symbols 
    if any(not char.isalnum() for char in password):
        print("Password cannot contain symbols")
        return
    
    #make sure password is at most 12 characters 
    if len(password) > 12:
        print("Password cannot be more than 12 characters")
        return
    
    
    with open("credentials.txt", "w") as f:
        f.write(f"{username}\n{password}")

def ReadUsernamePassword():
    with open("credentials.txt", "r") as f:
        username = f.readline().strip()
        password = f.readline().strip()
    return username, password

def login():
    username, password = ReadUsernamePassword()
    input_username = input("Enter username: ")
    input_password = input("Enter password: ")
    if input_username == username and input_password == password:
        print("Login successful")
    else:
        print("Login failed")

def register():
    username = input("Enter username: ")
    password = input("Enter password: ")
    StoreUsernamePassword(username, password)
    print("Registration successful")