Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
print("Welcome to randomprogram.....to enter please provide Username and Password")
def main():
    a = open('C:/Users/josep/Documents/randomprogram(acc).txt','r')#here the info is stored which happens to be user123
    b = a.readlines(1)
    c = a.readlines(2)
    c = [a.strip() for a in c]
    print(c)
    d = input("Username:")#i type user
    e = input("Password:")#i type 123
    f = (d + e)
    print(f)
    if f == c:
        g = a.readlines(1)
        g = [a.strip() for a in g]
        print("Welcome to randomprogram" + f)
    else:
        print("Wrong Username and/or Password.....try again")
        main()
main()


What I have tried:

ive tried searching on the internet but cant find a solution
Posted
Updated 18-Jun-18 16:26pm
v3

1 solution

You have got many thing wrong with your code. I will just mention and explain the issues

Python
print("Welcome to randomprogram.....to enter please provide Username and Password")
def main():
    a = open('C:/Users/josep/Documents/randomprogram(acc).txt','r')
    # 1. readlines read all the lines and return. And `a` object's offset set to last. 
    # 2. readlines work with no parameter too. In case of no parameter it will return all the items. if you put number it will start reading from the number you have put. and indexing starts from 0. a.readlines(1) will read from second line, skipping first line.
    b = a.readlines(1)
    # if you want to do readlines operation second time you need to set the offset back to start. Read the document http://python-reference.readthedocs.io/en/latest/docs/file/seek.html about this
    c = a.readlines(2)
    c = [a.strip() for a in c] # why did you use a? you are already using a for file handling. Try other variable name. and for your information both b and a are array
    print(c)
    d = input("Username:") #i type user
    e = input("Password:") #i type 123
    f = (d + e)
    print(f)
    if f == c: # c is array, should be handled as array 
        # from your coding style i am guessing. line 1 contains the program name and second line contains username and password. and you tried to read that one in variable b.
        g = a.readlines(1)
        g = [a.strip() for a in g]
        print("Welcome to randomprogram" + f)
    else:
        print("Wrong Username and/or Password.....try again")
        main()
main()

My suggestion will be read all lines as are doing and the put all the line in proper variables; example:
Python
a=open(fname,'r')
(programName, userpassword)=[f.strip() for f in a.readlines()]
print(programName);
print(userpassword);

* seek — Python Reference (The Right Way) 0.1 documentation[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900