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(‘:’)
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