Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Values not split. "not enough values to unpack (expected 2, got 1)" when going through my text file line by line

def player_login():

    username = ""
    new = "0"

    while new not in ('1', '2'):
        print("Type 1 to Login")
        print("Type 2 to Register")
        new = input("")

    if new == '1':
        login_username = input('username: ')
        login_password = input('password: ')
        found_username = False

        with open('user_database.txt', 'r') as password_file:
            for line in password_file:
                username, password = line.strip().split(':')

                if login_username == username:
                    found_username = True
                    if login_password == password:
                        print('success!')
                    else:
                        print('login failure!')
                    break

        username = login_username
        if not found_username:
            print('username invalid')
    if new == '2':
        #code continues
player login()

This is the error message I get:

line 18, in player_login

username, password = line.strip().split(‘:’)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ValueError: not enough values to unpack (expected 2, got 1)

Its weird because this code used to work until I switched from PyCharm to VSC and I cannot figure out the problem. Any help is appreciated.

This is my text file if it helps:

asdaasda:asdaasda
asdfasdf:asdfasdf
hellohi:hellohi

>Solution :

user_database.txt has some line that is missing : (perhaps an empty line)

A simple fix is often to skip blank lines

    ...
    for line in fh:
        if not line:
            continue
        ...

It’s also often helpful to enumerate() to report bad lines

for lineno, line in enumerate(fh, 1):
    ...
    if (<some validations fails>):
        print(f"bad line {lineno}: {line}")  # raise ValueError? to taste
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading