Jump to content

So this is probably really obvious, but how do you split a line in a file into two separate strings. My current code is:

def authentication():
    username = input("Please enter your username")
    password = input("Please enter your password")  
    for line in open("users.txt","r").readlines(): # Read the lines
        login_info = line.split() # Split on the space, and store the results in a list of two strings
        if username == login_info[0] and password == login_info[1]:
            print("Correct credentials!")
            rollDieWithPoints()
    print("Incorrect credentials.")

My formatting for my file is 'username, password'.

 

Also how can I change this code so it will accept two users!

 

Thanks!

Link to comment
https://linustechtips.com/topic/1020117-python-file-line-split/
Share on other sites

Link to post
Share on other sites

To put the login_info into two different strings, you can just use:

username_test, password_test = line.split() # space is the default, but you can put ", " into the parentheses to split by the comma and space.

What do you mean, "accept two users"? Do you mean test for two users (and two passwords) at once?

Fan Comparisons          F@H          PCPartPicker         Analysis of Market Trends (Coming soon? Never? Who knows!)

Designing a mITX case. Working on aluminum prototypes.

Open for intern / part-time. Good at maths, CAD and airflow stuff. Dabbled with Python.

Please fill out this form! It helps a ton! https://linustechtips.com/main/topic/841400-the-poll-to-end-all-polls-poll/

Link to comment
https://linustechtips.com/topic/1020117-python-file-line-split/#findComment-12180589
Share on other sites

Link to post
Share on other sites

1 minute ago, Imbellis said:

To put the login_info into two different strings, you can just use:


username_test, password_test = line.split() # space is the default, but you can put ", " into the parentheses to split by the comma and space.

What do you mean, "accept two users"? Do you mean test for two users (and two passwords) at once?

How do i put that into my code, and yes test for two users to be authenticated then let them play the game (which i have already written).

Link to comment
https://linustechtips.com/topic/1020117-python-file-line-split/#findComment-12180603
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×