即使我同样输入它,我也不能使f等于C,请解释原因 [英] I cant get f to equal C even when I typed them equally please explain why

查看:78
本文介绍了即使我同样输入它,我也不能使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()





我尝试过:



我试过了在互联网上搜索,但无法找到解决方案



What I have tried:

ive tried searching on the internet but cant find a solution

推荐答案

你的代码有很多问题。我将只提及并解释问题



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

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:

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文档 [ ^ ]


这篇关于即使我同样输入它,我也不能使f等于C,请解释原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆