Member 13877260 Ответов: 1

Я не могу получить f равным C даже когда я набрал их одинаково пожалуйста объясните почему


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()


Что я уже пробовал:

я пробовал искать в интернете, но не могу найти решение

1 Ответов

Рейтинг:
1

Mohibur Rashid

У вас много чего не так с вашим кодом. Я просто упомяну и объясню эти вопросы

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()

Мое предложение будет читать все строки, как это делается, и помещать все строки в соответствующие переменные; пример:
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 документация[^]