Python Forum
Writing incorrect passwords to a file till its correct
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing incorrect passwords to a file till its correct
#1
# 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()
Reply
#2
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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
And please use python tags around your code. I added them for you this time.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I am getting an incorrect average, and not sure why? What's wrong with my code? shirleylam852 8 4,664 Nov-20-2020, 05:32 AM
Last Post: deanhystad
  Hashing Passwords (HELP) MianDoesCoding 4 2,168 May-26-2020, 03:11 PM
Last Post: buran
  How to make the function to prompt the user repeatedly till he enters a no sbabu 3 2,411 Mar-26-2020, 10:04 PM
Last Post: Blackdog31
  Multiple Passwords NotPythonQueen 17 6,685 Sep-22-2019, 06:08 PM
Last Post: gbs
  Correct errors in text file. pawlo392 2 2,234 Apr-17-2019, 07:55 PM
Last Post: pawlo392
  reading text file and writing to an output file precedded by line numbers kannan 7 10,366 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Return giving incorrect value Calingaladha 5 3,341 Oct-24-2018, 09:53 PM
Last Post: LeSchakal
  Writing to a .py file J0k3r 3 3,359 Apr-18-2018, 07:01 PM
Last Post: woooee
  Incorrect code janek30 11 8,396 May-09-2017, 10:06 AM
Last Post: janek30

Forum Jump:

User Panel Messages

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