1

So I've been trying to create a quiz where you have to have an account which means you can register and login. I managed to code the register part(which i'm pretty proud of) and it saves the login details to a separate text file, when it saves the login details it looks like this: username:password

Now i'm struggling with the login part, I think you have to read the text file and then split the username and password, then I some how have to compare the inputted username and password to the saved ones. This is what I done for the login part so far but it doesn't work:

def login():
filename = 'Accounts.txt'
openfile = open('Accounts.txt', "r")
Userdata = openfile.readlines()
with open('Accounts.txt', 'r') as file:
    for line in file:
        user2, passw = line.split(':')
        login2 = input("Enter username: ")
        passw2 = input("Enter passwordd: ")
        if login2 == user2 and passw2 == passw:
            print("Logged in")
        else:
            print("User or password is incorrect!")
openfile.close();

Now this is how the whole code looks like(if needed):

import time

print("Welcome to my quiz")

#Creating username
def create():
    print ("We will need some information from you!")
    time.sleep(1)
    Veri = input("Would you like to continue (yes or no): ")
    def createAccount():
        while True:
            name = input("Enter your first name: ")
            if not name.isalpha():
                print("Only letters are allowed!")
            else:
                break
        while True:
            surname = input("Enter your surname: ")
            if not surname.isalpha():
                print("Only letters are allowed!")
            else:
                break
        while True:
            try:
                age = int(input("Enter your age: "))
            except ValueError:
                print("Only numbers are allowed!")
                continue
            else:
                break
        if len(name) >= 3:
            username = name[0:3]+str(age)
        elif len(surname) >= 3:
            username = surname[0:3]+str(age)
        else:
            username = input("Create a username: ")
        print ("Your username is:",username)
        while True:
            password = input("Create a password: ")
            password2 = input("Confirm your password: ")
            if password != password2:
                print("Password does not match!")
            else:
                break 
        account = '%s:%s\n'%(username,password)
        with open ('Accounts.txt','a') as file:
            file.write(account)
            print ('Account saved')   
    if Veri == 'no':
        menu()
    elif Veri == 'yes':
        createAccount()
    else:
        print ("Sorry, that was an invalid command!")
    time.sleep(1)
    login()

#Loging in
def login():
    filename = 'Accounts.txt'
    openfile = open('Accounts.txt', "r")
    Userdata = openfile.readlines()
    with open('Accounts.txt', 'r') as file:
        for line in file:
            user2, passw = line.split(':')
            login2 = input("Enter username: ")
            passw2 = input("Enter passwordd: ")
            if login2 == user2 and passw2 == passw:
                print("Logged in")
            else:
                print("User or password is incorrect!")
    openfile.close();

time.sleep(1)

#Choosing to log in or register
def menu():
    LogOrCre = input("Select '1' to login or '2' to register: ")
    if LogOrCre == '1':
        login()
    elif LogOrCre == '2':
        create()
    else:
        print ("Sorry, that was an invalid command!")
        menu()

menu()

If you have any ideas on how I can make the login part, that would be helpful.

  • _it doesn't work_ is not a good description of the problem. – Johnny Mopp Dec 12 '17 at 21:25
  • The flow of the code is back-to-front. You don't want to ask the user for their username and password _inside_ the `for line in file:` loop. Do that once before the loop. – roganjosh Dec 12 '17 at 21:26
  • Beyond that, there could be any number of problems, for example whitespace. You can call `strip()` on the items saved in your file as well as the user input but that also removes deliberate whitespace from the password. What is an example input that fails? – roganjosh Dec 12 '17 at 21:29

3 Answers3

1

You are asking for the user's username and password for every line in the accounts file. Get the inputted login information outside of the for loop.

zrh
  • 81
  • 5
  • 8
  • 1
    OP also needs to break out of the loop on success. And only print the success/failure message after the loop has ended. – Johnny Mopp Dec 12 '17 at 21:30
1

Welcome to programming! It can be very daunting, but keep studying and practicing. You are in the right way!

I see some points in your code that could be improved. I'm commenting below:

1) You don't need to use both open() (and close()) and with to access a file's contents. Just use with, in this case. It will make you code simpler. (A good answer about with)

2) You're asking for the user login & passwd inside a loop (aka multiple times). It can be very annoying for your user. Move the input's call to before the for loop.

3) You also need to break the loop when the login succeeds.

So, a slightly improved version of your code would be:

filename = 'Accounts.txt'
with open(filename, 'r') as file:
    login2 = input("Enter username: ")
    passw2 = input("Enter password: ")

    for line in file:
        user2, passw = line.split(':')
        if login2 == user2 and passw2 == passw:
            print("Logged in")
            break
        else:
            print("User or password is incorrect!")
Thiago Curvelo
  • 3,635
  • 1
  • 24
  • 37
0

Not sure what is not working in your code, but I updated the code as below and was able to print logged in.

def login():
#filename = 'Accounts.txt'
#openfile = open('Accounts.txt', "r")
#Userdata = openfile.readlines()
with open('Accounts.txt', 'r') as file:
    login2 = input("Enter username: ")
    passw2 = input("Enter passwordd: ")
    for line in file:
        user2, passw = line.split(':')
        if login2 == user2 and passw2 == passw:
            print("Logged in")
            break
        else:
            continue

login()