Python Forum
Writing incorrect passwords to a file till its correct - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Writing incorrect passwords to a file till its correct (/thread-2006.html)



Writing incorrect passwords to a file till its correct - garth - Feb-10-2017

# Now, create a Python file called forgetful.py . Imagine your friend was very forgetful and always seemed to enter his email password incorrectly. You want to write a Python program that takes all his incorrect password entries, stores it in a list, then records 
# all his incorrect password entries in a text file called wrongpasswords.txt. 

# Example: your friends password is 'rusty'. But he enters 'rusty123', 'Rusty', 'rustless' before finally remembering his password is 'rusty' and entering it correctly.

# In this situation wrongpasswords.txt should read this exactly:
# Incorrect password 1: rusty123
# Incorrect password 2: Rusty
# Incorrect password 3: rustless
# Correct password entered on 4th entry.

# The program should ask the user for input by saying 'Please enter your password'. The correct password will always be 'rusty' but the user can of course enter any String.
# Good luck!

i am suppose to write entered passwords to the file till the "Correct" one is inputted which is "rusty" but i cant seem to get it right and ive spent over a couple of hours trying.. please help this is what i have coded:
enteredPass = raw_input("Enter your password: ")
incorrectPass= file("wrongpasswords.txt","w")                      
counter = 0



for i in range(0, counter+1):
    
    if enteredPass != "rusty":
        counter = counter +1
        incorrectPassO = open("wrongpasswords.txt","w")
        incorrectPassO.write("Incorrect password" +str(counter)+": " + enteredPass + "\n")
        incorrectPassO.close()
        enteredPass = raw_input("Enter your password: ")
        
        
    else:
        incorrectPassO = open("wrongpasswords.txt","w")

        incorrectPassO.write("Correct password entered on the " + str(counter)+"th entry")
        incorrectPassO.close()



RE: Writing incorrect passwords to a file till its correct - Ofnuts - Feb-10-2017

1) The part where you ask for the password should be in the loop... otherwise you ask the password only once.

2) Either open wrongpasswords.txt once for the whole program (ie, before the loop) or open it in 'a' mode each time in the loop. As it is done no you overwrite the file contents each time you write to it.


RE: Writing incorrect passwords to a file till its correct - ichabod801 - Feb-10-2017

And please use python tags around your code. I added them for you this time.