Python Forum
How can I fix this code so if i want to retry, it will go through the same loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I fix this code so if i want to retry, it will go through the same loop
#1
I want to make it so that if I get the password wrong, and I choose to retry, it will ask me again and go through the same checking process again.

logged_in=False
attempts=0

enter_name=input('ENTER NAME: ')
enter_password=input('ENTER PASSWORD ')

with open('Data.txt', 'r') as file:
    for line in file:
        name, password = line.split(',')
        if name == enter_name:
            logged_in = password == enter_password
            break
        else:
            attempts+=1
            print('WRONG USERNAME OR PASSWORD')
            enter_option=input('ENTER OPTIONS:\n A-RETRY\n')
if logged_in==True:
    print('Hello ',name)
Reply
#2
As it is you are checking the entered name against every name in the file, and failing if it doesn't match the name. You need to change the code to:

  1. Load the user names and passwords.
  2. Get a user name and password from the user.
  3. See if the user name is in the file (error if it isn't)
  4. See if the password matches the one in the file for that user name (error if it doesn't)

Once that is corrected, you could allow retries by putting a while loop around the last three steps, and breaking out of the loop when you get a correct user name/password combo, or too many attempts.
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
Question Use function, retry until valid Ashcora 8 1,465 Jan-06-2023, 10:14 AM
Last Post: Ashcora
  How to ignore "Retrying (Retry(total=2, connect=2, read=5, redirect=5, status=None))" const 3 2,694 Mar-26-2022, 08:55 AM
Last Post: ndc85430
  requests_futures.sessions retry list of HTTP 429 Johanoosterwaal 0 1,555 Nov-20-2020, 09:23 PM
Last Post: Johanoosterwaal
  Retry After Exception Harshil 2 2,247 Aug-09-2020, 05:32 PM
Last Post: Harshil

Forum Jump:

User Panel Messages

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