Ive been trying to store the newly created users password into its designated username for my login system project however, type error keeps occuring and im not to sure how to change my password value into an integer or resolve the problem. Thank you for helping

user = []
status = ""

status = input("Have You Registered With Our System? y/n")

username = input("Choose A Username: ")

if username in user:
        print("Username is already in use")
else:
        createPassword = input("Create Your Password: ")
        user[username] = createPassword
        print("\n User Has Been Created \n")


        login = input(" Enter login name: ")
password = input( " Enter Password: ")

if login in user and user[login] == password:
        print("\n Successfully loged in\n")
        #with open("user.txt", "w") as f:
        f = open("user.txt", "a")
        #print(f.mode)
        #f.close()
        f.write(login)
        f.write(" ")
        f.write(password)
        f.write(" ")
        f.close()

else:
        print("\n User Does Not Exist or Wrong Features")

3 Answers

0
loknoq On

Your user is a python list. Simply use a python dictionary as you intended to do and your code works perfectly:

First line:

user = {}
0
C.Nivs On

The reason you are hitting a TypeError is because lists are indexed with integer values, rather than strings.

I think a dictionary might be a bit more helpful here, as you can store strings, ints, even tuples as keys (but not lists, sets, or dicts):

users = {}

username = 'testuser'
password = '1234knaoiva'

if username in users:
    if password != users[username]:
        raise ValueError("Invalid Password")
    else:
        print("Login successful")

else:
    users[username] = password
    print("Welcome, new user!")

This has a few advantages. First, tests for membership in a dict are much faster for large amounts of data, and doesn't change as the size of the dictionary grows. Whereas testing a list for membership grows the more values (or in your case, users) it contains

To show how this works in your case:

users = {} # Users is an empty dictionary

username = input("What's your username? ") # testuser

username in users
# False
# Fails membership test

users[username] = input("What's your password? ") # testpass
users

# {'testuser': 'testpass'}

users[username]
# testpass

To show the performance difference over membership tests (worst case) on small versus large groups of data:

100 Elements, Worst Case

# List
python -m timeit -s 'd = [i for i in range(100)]' '999999 in d'
1000000 loops, best of 3: 0.801 usec per loop 

# Dict
-m timeit -s 'd = {i: None for i in range(100)}' '999999 in d'
10000000 loops, best of 3: 0.0262 usec per loop

1000000 Elements, Worst Case

# List
-m timeit -s 'd = [i for i in range(1000000)]' '999999 in d'
10000000 loops, best of 3: 8.34 msec per loop

# Dict
-m timeit -s 'd = {i: None for i in range(1000000)}' '999999 in d'
10000000 loops, best of 3: 0.0343 usec per loop

For smaller amounts of data you don't see too much of a difference (on a modern processor), though even at only 100 elements, dict is faster by an order of magnitude. For larger amounts, the difference is astronomical

0
William Torkington On

That's because username's type is 'str' and not int, which you can't convert to int unless the username is all 0's and 1's.

I recommend using a dictionary for holding usernames and passwords:

user = {}

# in creating password:
    createPassword = input("Create Your Password: ")
    user[username] = createPassword // This works because dictionary keys can be str
    print("\n User Has Been Created \n") 

Then to check if a username is inside of the dictionary you use if name in user.keys():