user3788435
user3788435

Reputation: 37

Python if statement saying 2 strings are not the same

import time
#settings file (file with username/password)
settings = "settings.txt"
#set true to skip login
login = False
while not login:

    print("""
                        /------------------------------\\\n
                        /                              \\\n
                        /  Please Enter Your Username  \\\n
                        /                              \\\n
                        /                              \\\n
                        /------------------------------\\\n
    """)
    username = input()
    print("""\n\n\n\n\n\n\n\n
                        /------------------------------\\\n
                        /                              \\\n
                        /  Please Enter Your Password  \\\n
                        /                              \\\n
                        /                              \\\n
                        /------------------------------\\\n
    """)
    password = input()
    #open file in read and write mode
    file = open(settings, "r+")
    for line in file:
        if "username: " in line:
            line = line.replace("username: ", "")
            if username == line:
                print("test")
file.close()

when i run this and fill it all in it will not print test i am typeing in username but it will not work. it gets to if username == line: print("test") and does not print test

The Script/application is for homework at my school

this is the settings file

username: username
password: password

Upvotes: 1

Views: 95

Answers (2)

kalugny
kalugny

Reputation: 1349

input is not the correct function to use here, as it evaluates the value the user entered. Actually, it is generally unsafe to use it at all.

  1. Replace input with raw_input.
  2. Make sure to strip each string to remove unseen characters (like space and \n).

Upvotes: 2

Kasravnd
Kasravnd

Reputation: 107287

first you need to check username as string if it is in txt file! then you have \n at the end of your line or using line.strip() for removing the '\n'! so change the if statement to :

if username+'\n' == line:  #if user name is txt use 'username' 

or

if username == line.strip():

Upvotes: 2

Related Questions