Python Forum
New to coding. An error occurs with no definition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to coding. An error occurs with no definition
#1
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
Reply
#2
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
Reply
#3
(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?
Reply
#4
Strip, i.e. remove it, not replace it.
Reply
#5
(Mar-15-2018, 12:36 PM)buran Wrote: Strip, i.e. remove it, not replace it.
Righty 'o
Thank you very much :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Return a definition samh625 6 2,441 Jul-28-2020, 08:32 AM
Last Post: samh625
  Coding error? Mugwash 4 3,130 Apr-06-2018, 06:57 PM
Last Post: Mugwash
  trouble importing class definition from one .py into another ijosefson 3 3,336 Oct-16-2017, 08:24 AM
Last Post: Larz60+
  Python Coding Error MGallo 3 3,770 Jul-20-2017, 02:35 PM
Last Post: MGallo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020