Python Forum

Full Version: New to coding. An error occurs with no definition
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to create a user system for my program and I have encountered an error. Any help?
This is the code:
def main():
    loginorregister = input("What would you like to do?\n1. Login\n2. Register\n")
    if loginorregister == "1":
        nameinput = input("Please enter your username: ")
        passinput = input("Please enter your password: ")
        file2 = open("userdata.csv","r")
        for line in file2:
            details = line.split(",")
            print(details[0])
            print(details[1])
            if details[0] == nameinput:
                if details[1] == passinput:
                    print("Welcome!")#Currently broken, No response, working on a solution.
    elif loginorregister == "2":
        usernamename = input("Please enter your name: ")
        usernamenumber = input("Please enter your age: ")
        usernameyear = input("Please enter your year group: ")
        password = input("Please enter a password: ")
        username = usernamename[0:3] + usernamenumber + usernameyear
        print("Your username is: ", username)
        file1 = open("userdata.csv","a")
        file1.write(username + "," + password + "\n")
        file1.close()
        print("Userdata set correctly.")
    else:
        print("ERROR, RESTARTING PROGRAM.")
        main()
main()

if loginorregister == "1":
        nameinput = input("Please enter your username: ")
        passinput = input("Please enter your password: ")
        file2 = open("userdata.csv","r")
        for line in file2:
            details = line.split(",")
            print(details[0])
            print(details[1])
            if details[0] == nameinput:
                if details[1] == passinput:
                    print("Welcome!")#Currently broken, No response, working on a solution.
This part (Line 30) should read the userdata.csv file and print welcome if their username and password were correct, as well as printing the username and password. However, the program does not print welcome, Just the username and password. I have tried removing "print(details[0])" and "print(details[1])" and It comes up with the same blank space where welcome should be.

Does anyone know a fix to this? Huh

[Image: EFDp]
This is what happens.

def main():
    loginorregister = input("What would you like to do?\n1. Login\n2. Register\n")
    if loginorregister == "1":
        nameinput = input("Please enter your username: ")
        passinput = input("Please enter your password: ")
        file2 = open("userdata.csv","r")
        for line in file2:
            details = line.split(",")
            print(details[0])
            print(details[1])
            if details[0] == nameinput:
                if details[1] == passinput:
                    print("Welcome!")#Currently broken, No response, working on a solution.
    elif loginorregister == "2":
        usernamename = input("Please enter your name: ")
        usernamenumber = input("Please enter your age: ")
        usernameyear = input("Please enter your year group: ")
        password = input("Please enter a password: ")
        username = usernamename[0:3] + usernamenumber + usernameyear
        print("Your username is: ", username)
        file1 = open("userdata.csv","a")
        file1.write(username + "," + password + "\n")
        file1.close()
        print("Userdata set correctly.")
    else:
        print("ERROR, RESTARTING PROGRAM.")
        main()
main()
Output:
What would you like to do? 1. Login 2. Register 1 Please enter your username: Jas1611 Please enter your password: letmein222 Jas1611 letmein222
you are reading the username and password from a file. thus line ends whit new line \n. you need to strip that new line or your password (details[1]) will not match, i.e. you expect it to be letmein222, but it is letmein222\n
(Mar-15-2018, 12:26 PM)buran Wrote: [ -> ]you are reading the username and password from a file. thus line ends whit new line \n. you need to strip that new line or your password (details[1]) will not match, i.e. you expect it to be letmein222, but it is letmein222\n

so would I need to remove "\n" or put something in its place or before it?
Strip, i.e. remove it, not replace it.
(Mar-15-2018, 12:36 PM)buran Wrote: [ -> ]Strip, i.e. remove it, not replace it.
Righty 'o
Thank you very much :)