Python Forum
Why wont this work :( - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why wont this work :( (/thread-13835.html)



Why wont this work :( - Nonagon - Nov-02-2018

its a username/password log in an external file but it only stores some stuff and doesnt work properly.It adds the second username twice even though its already in the file and if i do it so the first input was in the file and the second one wrong, then it works. It also works sometimes.
I want the code to store the username and password (togther) if the username isnt in the fils and just print"Validtaed" if the username and password is in teh file.



import sys 
username1 = input("Player one enter an existing or new username of 8 characters: ")
if len(username1)== 8:
     you = 2
else:
    print("Invalid username try again")
    sys.exit()
password1 = input("Player one enter an existing or new password of 4 characters: ")
if len(password1)== 4:
     you = 2
else:
    print("Invalid password try again")
    sys.exit()
username2 = input("Player two enter an existing or new username of 8 characters: ")
if len(username2)== 8:
     you = 2
else:
    print("Invalid username try again")
    sys.exit()

password2 = input("Player two enter an existing or new password of 4 characters: ")
if len(password2)== 4:
     you = 2
else:
    print("Invalid password try again")
    sys.exit()


usernameandpassword1 = username1+password1
usernameandpassword2 = username2+password2

#Checks if Username and Password are already known
file = open("Users.txt","r")
print(file.read())


for TextLine in file:
      if usernameandpassword1 in TextLine:
          FoundPhrase = TextLine
          print("Validated Password.")
          break
      else:
          #if the username and password isnt known itll add the account to the file
          with open('Users.txt', 'a') as tf:
              tf.write("\n"+ usernameandpassword1)
              print("Your account has been added.")
              
              
              


#same for user 2
f = open("Users.txt","r")
for Test in f:
      if (usernameandpassword2 in Test)== True:
          FoundPhrase = Test
          print("Validated Password.")
          break
      else:
          with open('Users.txt', 'a') as tf:
              tf.write("\n"+ usernameandpassword2)
              print("Your account has been added.")
              break



RE: Why wont this work :( - j.crater - Nov-02-2018

Please put your code in Python code tags. Also explain better what the problem is. If you get an error, post full error traceback message in error tags. You can find help about tags here. If you get undesired result, give an example of input you use and the result you want to have.


RE: Why wont this work :( - wavic - Nov-03-2018

Hm! I am not sure what happens when you try to open a file for append already opened for a reading mode.
Use the with statement to open the file:
with open('file.txt', 'r') as f:
    # do something

with open('file.txt', 'a') as f:
    # do something else
Also in this loop
file = open("Users.txt","r")
print(file.read())
 
for TextLine in file:
      if usernameandpassword1 in TextLine:
          FoundPhrase = TextLine
          print("Validated Password.")
          break
      else:
          #if the username and password isnt known itll add the account to the file
          with open('Users.txt', 'a') as tf:
              tf.write("\n"+ usernameandpassword1)
              print("Your account has been added.")
if usernameandpassword1 is not in the first line the account will be added to the file. What if the account is in the second line?
Same with the other loop.

When you have to do something twice or more times, write a function.