#reg.py
#Choose username and password
def regis():
    userpass = open("userpass.txt","w")
    userpass1 = userpass.write(raw_input("Please select a username: ") + "\n")
    password1 = userpass.write(raw_input("Select a password: "))
    userpass.close()
#end of reg.py

#login.py

# Should i import reg before this function or is it ok to do it in the function?

# This should read the file and allow you to login if your user and pass match the ones registered

# I am having trouble with these things:

#  (A) The login prompt works completely fine with the file already created(registered user and pass)//
# -    But not when the file hasn't been written//
# -    I am trying to call the regis function (which works) ONLY IF the file has nothing in it/hasn't been created//

#  (A-2) if i try to integrate registration into this file, as opposed to an import//
# -      everything works as it is supposed to EXCEPT the login loops around on itself and continues to ask//
# -      for the information after it has already confirmed it... not sure why, i tried messing with all my loops??

def login():
    import reg
    begin = open("userpass.txt", "w") # This is here because it would not use regis() if no file was ever created//
                                      #-As opposed to just being an empty file. 
    username = open("userpass.txt", "r")
    username.seek(0,0)
    for line in username.readlines():
        if line == 0: # - This will not call regis// ??works different when you call for amount of text in a file??
                      
            reg.regis()

            # opens the files... if there is data in the files it prompts for user and pass
            # I have tried to different setups... either it doesn't load at all// when finished it loops the top of login()

        username = open("userpass.txt", "r")
        username.seek(0,0)
        for line in username.readlines():
            if line != 0:

                username.seek(0,0)
                a = username.readline()
                username.seek(0,1)
                b = username.readline()
                e = 0
                f = 0
                u = raw_input("Username: ") + "\n"
                while e == 0:
    
                    if u == a:
                        p = raw_input("Password: ")
                        while f == 0:
                            
                    
                            if p == b:
                                e = 1                                                             
                                f = 1
                                z = 1
                                print "You are now logged in as: " + a

                    
                        
                            else:
                                f = 0
                                p = raw_input("Incorrect password... Try Again: ")

                    else:
                        u = raw_input("Sorry that is not a registered username... Try again: ") + "\n"
                        e = 0
            

login()
raw_input("End of test, press enter.")

This is Z... Thanks in advance to anyone who will offer help... I have only been programming for a day so yeah, trying to wrap my head around why this isn't working. I have tried various configs with this program, i want to do separate modules so that I could create a class to create multiple registrations... but I wanted to do it one step at a time instead of trying to do the whole thing and be lost for a year in debugging. In each of my variations I have either got it to work, but it loops to the login screen infinitely, or it will login but if no one is registered it will no call the registration function. So either way it doesn't work like I wanted it to. Thanks again.

~Z~

Maybe append mode 'a' would be more appropriate than starting with empty file?
Generally easier approach would be to read in all passwords, create new file in except clause if it is first user. Put used usernames in set and check from that if user name is available. After succesfull input off username and password two times append new user at end of file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.