-1

I am writing a program to check the user id from a file. But when i run the program, it just runs the first print statement and gets stuck there:

print ("Hello!")
print('Would you like to Login or Create an Account?')
choice = input('(Create Account/Login)')

#For creators of new account
if choice.casefold() == "create account":
    with open("credentials_dict.txt", 'a+') as file:
        line = file.readlines()

        #Entering User ID
        print("User ID Should be 3 to 6 Alphanumeric characters")
        uservalidity = False
        while not uservalidity:
            for line in file.readlines():
                userid = input('Enter User ID')
                check_user = checkuserid(userid,line)
                if check_user:
                    print ("Valid Username")
                    uservalidity = True
                else:
                    print ("Incorrect Format or UserID exists")

The output is:

Hello!
Would you like to Login or Create an Account?
(Create Account/Login)Create Account
User ID Should be 3 to 6 Alphanumeric characters

The error message when i end the execution abruptly: File "C:\Users\mahev\Downloads\Python Files\Login_application.py", line 57, in for line in file.readlines():

File "C:\ProgramData\Anaconda3\lib\encodings\cp1252.py", line 22, in decode
def decode(self, input, final=False):

That's it the program doesn't progress further. Any pointers?

  • If your program is in a tight loop and you force it to quit, any exception you get on the way out is probably not that useful. – Karl Knechtel Jun 22 '20 at 00:10

3 Answers3

1

There are two things that you need to correct

  1. Open file in read mode i.e.with open("credentials_dict.txt", 'r')
  2. you have already read all lines in line = file.readlines() so when you do this for line in file.readlines(): inside while loop, you will get an empty list and you wouldn't go inside for loop. change your statement to for line in lines: it will work.
jawad-khan
  • 313
  • 1
  • 10
0

The readlines() method returns a list containing each line in the file as a list item. So when you first call line = file.readlines() you read the whole file. Then, later, inside the loop while not uservalidity, when you do for line in readlines(): your loop always returns an empty list and therefor it never executes the code inside the for loop. That makes your code endelessly looping inside the while loop. You should consider using the readline() method instead of readlines().

Aristotelis V
  • 396
  • 1
  • 9
0

From a first glance, it looks like you called file.readlines() twice. When you first call it the position of the reader is 0 before the first character and so it read all of the content into your variable “line” and the reader/pointer is left at the last character of your file. So when you call file.readlines() again there are no lines and it returns an empty list as the pointer as at the end of the file. You can use

file.seek(0)

Before your for loop to get it to work again or loop through the variable “line” that you assigned with file.readlines()

for line in line:  # you should change that variable name to lines to make it more clear 
Eno Gerguri
  • 639
  • 5
  • 22