Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why wont this work :(
#1
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
Reply
#2
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.
Reply
#3
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why wont this path work one way, but will the other way? cubangt 2 623 Sep-01-2023, 04:14 PM
Last Post: cubangt
  Why wont subprocess call work? steve_shambles 3 2,605 Apr-28-2020, 03:06 PM
Last Post: steve_shambles
  When I import a Module it wont run PyNovice 17 6,244 Oct-26-2019, 11:14 AM
Last Post: PyNovice
  Why wont this work? ejected 2 3,143 Mar-29-2019, 05:33 AM
Last Post: snippsat
  wont print last sentence.. mitmit293 2 2,324 Jan-27-2019, 05:38 PM
Last Post: aakashjha001
  Boolean Wont Work fierygaming 5 4,142 Jun-13-2018, 07:49 PM
Last Post: fierygaming
  Reddit Bot comes up with no errors but wont actually run Rubix3D 0 2,589 Jan-12-2018, 12:53 AM
Last Post: Rubix3D

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020